代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <string.h>
#include "base.h"
#include "icommand.h"
#include "iqueue.h"
#include "iblockingqueue.h"
#include "linkedqueue.h"
#include "linked_blockingqueue.h"
static void LinkedBlockingQueue_push(IQueue*, ICommand*);
static ICommand* LinkedBlockingQueue_pop(IQueue*);
static int LinkedBlockingQueue_empty(IQueue*);
LinkedBlockingQueue* LinkedBlockingQueue_construct(void* addr)
{
if (addr == NULL) {
return NULL;
}
LinkedBlockingQueue* linkedBlockingQueue = addr;
linkedBlockingQueue->queue = &new(LinkedQueue)->iqueue;
pthread_mutexattr_t attr;
int err = pthread_mutexattr_init(&attr);
if (err) {
fprintf(stderr, "pthread_mutexattr_init error. [err=%s]",
strerror(err));
return NULL;
}
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if (err) {
fprintf(stderr, "pthread_mutexattr_settype error. [err=%s]",
strerror(err));
return NULL;
}
err = pthread_mutex_init(&linkedBlockingQueue->mutex, &attr);
if (err) {
fprintf(stderr, "pthread_mutex_init error. [err=%s]",
strerror(err));
return NULL;
}
err = pthread_cond_init(&linkedBlockingQueue->cond, NULL);
if (err) {
fprintf(stderr, "pthread_cond_init error. [err=%s]",
strerror(err));
return NULL;
}
linkedBlockingQueue->push = LinkedBlockingQueue_push;
linkedBlockingQueue->pop = LinkedBlockingQueue_pop;
linkedBlockingQueue->empty = LinkedBlockingQueue_empty;
return linkedBlockingQueue;
}
void LinkedBlockingQueue_destruct(LinkedBlockingQueue* linkedBlockingQueue)
{
int err = pthread_cond_destroy(&linkedBlockingQueue->cond);
if (err) {
fprintf(stderr, "pthread_cond_destroy error. [err=%s]",
strerror(err));
}
err = pthread_mutex_destroy(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_destroy error. [err=%s]",
strerror(err));
}
LinkedQueue* linkedQueue =
container_of(linkedBlockingQueue->queue, LinkedQueue, iqueue);
delete(LinkedQueue, linkedQueue);
}
void LinkedBlockingQueue_push(IQueue* iqueue, ICommand* command)
{
LinkedBlockingQueue* linkedBlockingQueue =
container_of(iqueue, LinkedBlockingQueue, iqueue);
int err = pthread_mutex_lock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_lock error. [err=%s]",
strerror(err));
return;
}
linkedBlockingQueue->queue->push(iqueue, command);
err = pthread_mutex_unlock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_unlock error. [err=%s]",
strerror(err));
}
err = pthread_cond_signal(&linkedBlockingQueue->cond);
if (err) {
fprintf(stderr, "pthread_cond_signal error. [err=%s]",
strerror(err));
}
}
ICommand* LinkedBlockingQueue_pop(IQueue* iqueue)
{
LinkedBlockingQueue* linkedBlockingQueue =
container_of(iqueue, LinkedBlockingQueue, iqueue);
int err = pthread_mutex_lock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_lock error. [err=%s]",
strerror(err));
return NULL;
}
while (linkedBlockingQueue->empty(iqueue)) {
err = pthread_cond_wait(&linkedBlockingQueue->cond,
&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_cond_wait error. [err=%s]",
strerror(err));
return NULL;
}
}
ICommand* command =
linkedBlockingQueue->queue->pop(linkedBlockingQueue->queue);
err = pthread_mutex_unlock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_unlock error. [err=%s]",
strerror(err));
return NULL;
}
return command;
}
int LinkedBlockingQueue_empty(IQueue* iqueue)
{
LinkedBlockingQueue* linkedBlockingQueue =
container_of(iqueue, LinkedBlockingQueue, iqueue);
int isEmpty;
int err = pthread_mutex_lock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_lock error. [err=%s]",
strerror(err));
}
isEmpty = linkedBlockingQueue->queue->empty(linkedBlockingQueue->queue);
err = pthread_mutex_unlock(&linkedBlockingQueue->mutex);
if (err) {
fprintf(stderr, "pthread_mutex_unlock error. [err=%s]",
strerror(err));
}
return isEmpty;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。