int main (int argc, char *argv[]) { struct queue *queue; plan (NO_PLAN); if (!(queue = queue_create (true))) BAIL_OUT ("queue_create() failed"); single_job_check (queue); multi_job_check (queue); queue_destroy (queue); done_testing (); }
int queue_push(pqueuenode header,pqueueelem elem){ pqueuenode node = queue_create(); if(header->_tailer == NULL){ header->_tailer = node; header->_fronter = node; node->elem = *elem; } else { header->_tailer->_next = node; header->_tailer = node; node->elem = *elem; } header->size++; return 0; }
static void multi_thread_test(void) { pthread_t consumer_t; pthread_t producer_t; Queue* queue = queue_create(NULL, NULL); pthread_create(&producer_t, NULL, producer_run, (void*)queue); pthread_create(&consumer_t, NULL, consumer_run, (void*)queue); pthread_join(consumer_t, NULL); pthread_join(producer_t, NULL); consumer_run((void*)queue); }
static int chan_new(lua_State* L) { const char* name = _lua_arg_string(L, 1, NULL, _usage_new); int limit = _lua_arg_integer(L, 2, 1, 0, _usage_new); struct queue_t* q = queue_create(name, limit); if (!queues_add(q)) { queue_destroy(q); lua_pushnil(L); lua_pushstring(L, "chan name duplicated"); return 2; } chan_pushqueue(L, q); return 1; }
/*! * Creates a task which encapsulates a service. * The reason for this is to make it possible to observe services and to track * dependencies. A task can be both an observer and a subject since it uses * C inheritance from the \c struct \c subject_t. * * \param service - A service that is going to be encapsulated into a task. * * \return A task which encapsulates a service. */ task_t * task_create(struct service_t *service, struct task_handler_t *handler) { if (service->name != NULL) { task_t * this_ptr = (task_t*) malloc(sizeof(task_t)); if (this_ptr != NULL) { this_ptr->task_id = hash_generate(service->name); this_ptr->dependency_queue = NULL; this_ptr->service = service; this_ptr->task_handler = handler; this_ptr->counter = 0; subject_init((subject_t*) this_ptr); observer_set_notify((observer_t*) this_ptr, task_notify); /* Check if there is a provides string, if there isn't any provides * string, use the task name instead for the generated id. */ if (service->provides != NULL) { this_ptr->provides_id = hash_generate(service->provides); } else { this_ptr->provides_id = this_ptr->task_id; } if (service->dependency != NULL) { char **dependency_arg = (char**) service->dependency; task_dependency_t *dependency; this_ptr->dependency_queue = queue_create(); while (*dependency_arg != NULL) { printf("%s dep: %s\n",service->name, *dependency_arg); dependency = (task_dependency_t*) malloc(sizeof( task_dependency_t)); dependency->name = *dependency_arg; dependency->id = hash_generate(dependency->name); dependency->task = NULL; queue_push(this_ptr->dependency_queue, dependency); dependency_arg++; } } } return this_ptr; } return NULL; }
int main(int argc, char const *argv[]) { void * queue; queue = queue_create(); teacher_t t[50]; for (int i = 0 ; i < 50; i++) { t[i].age = i; queue_insert(queue, &t[i]); } teacher_t * p; int k = queue_length(queue); for (int i = 0; i < k-1; i++) { p = (teacher_t *)queue_delete(queue); fprintf(stdout, "%d ", p->age); } fprintf(stdout, "\n"); p = (teacher_t *)queue_head(queue); fprintf(stdout, "%d ", p->age); fprintf(stdout, "\n"); queue_delete(queue); for (int i = 0 ; i < 50; i++) { t[i].age = i + 100; queue_insert(queue, &t[i]); } if (!queue_empty(queue)) fprintf(stdout, "queue is not empty\n"); k = queue_length(queue); for (int i = 0; i < k; i++) { p = (teacher_t *)queue_delete(queue); fprintf(stdout, "%d ", p->age); } fprintf(stdout, "\n"); if (queue_empty(queue)) fprintf(stdout, "queue not empty\n"); queue_destroy(queue); return 0; }
int main(){ queue *Q; Q = queue_create(); int i ; for(i = 0;i < 5;i++){ queue_in(Q,i); } for(i = 0;i < 5;i++){ int test; test = queue_out(Q); printf("%d ",test); printf("\n"); } }
static void queue_test_enqueue(element_t e, int (*cmp)(element_t, element_t, int), int elem_sz) { queue_t q = queue_create(0); element_t elem; queue_enqueue(q, e); elem = queue_dequeue(q); assert(cmp(e, elem, elem_sz)); queue_delete(&q); fprintf(stdout, "Testing queue enqueue and dequeue success ...\n"); }
int inotifyStart() { if (inotify_fd > 0) { queue_t q; q = queue_create (128); process_inotify_events (q, inotify_fd); //} printf ("\nTerminating\n"); //close_inotify_fd (inotify_fd); queue_destroy (q); } return 0; }
struct pool *allocate_pool(size_t size) { debug("ALLOCATE-POOL", "allocating pool for size %d, pre-allocating %d slots (%d bytes)", size, prealloclen, prealloclen * (size + headerlen)); struct pool *p = (struct pool *) malloc(sizeof(struct pool)); p->freed = queue_create(); p->size = size; p->slots = prealloclen; p->cursor = 0; // pre-allocate some slots p->data = malloc((size + headerlen) * prealloclen); return p; }
int main() { queue *q = queue_create(8, NULL); assert(q != NULL); int a = 10; int b = 20; enqueue(q, &a); enqueue(q, &b); printf("size=%d\n", queue_size(q)); assert(queue_full(q) != true); printf("%d\n", *(int *)dequeue(q)); printf("%d\n", *(int *)dequeue(q)); assert(queue_empty(q) != false); queue_release(q); }
thread_pool_t *thread_pool_create(int size, thread_func_t func, void *data) { int i = 0; thread_pool_t *thread_pool = NULL; if(size <= 0){ return NULL; } thread_pool = (thread_pool_t *)MALLOC_WRAPPER(sizeof(thread_pool_t)); if(NULL == thread_pool){ printf("MALLOC_WRAPPER for thread pool failed.\n"); return NULL; } thread_pool->threads = (thread_context_t *)MALLOC_WRAPPER(sizeof(thread_context_t) * size); if(NULL == thread_pool->threads){ printf("MALLOC_WRAPPER for thread_pool->threads failed.\n"); thread_pool_destroy(&thread_pool); return NULL; } bzero(thread_pool->threads, sizeof(thread_context_t) * size); thread_pool->thread_num = size; thread_pool->queue = queue_create(1024); if(NULL == thread_pool->queue){ printf("queue_create for thread_pool->queue failed.\n"); thread_pool_destroy(&thread_pool); return NULL; } thread_pool->data = data; for(i = 0; i < size; i++){ thread_pool->threads[i].id = i; thread_pool->threads[i].thread_pool = thread_pool; thread_pool->threads[i].work_continue = 1; if(0 != pthread_create(&thread_pool->threads[i].thread, NULL, func, &thread_pool->threads[i])){ printf("create thread[%d] for thread pool failed.\n", i); thread_pool_destroy(&thread_pool); return NULL; } } return thread_pool; }
int append_apply_test(){ queue* q = queue_create(); int x = 0; int y = 1; int z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); queue_append(q, &x); printf("Queue size is %zu\n", queue_size(q)); int index = 0; queue_apply(q, show_one, &index); queue_destroy(q,false); return 0; }
int create_semaphore(int value) { int sem_index = get_available_semaphore_slot(); if (sem_index == -1) { printf("Error: maximum number of semaphores already in use.\n"); return -1; } /* Create a new semaphore. */ num_sem++; sem_t *sem = malloc(sizeof(sem_t)); sem->init = value; sem->count = value; sem->wait_queue = queue_create(); /* Insert the semaphore in the first empty slot in the table. */ sem_table[sem_index] = sem; return sem_index; }
int my_init_lib() { char CLIENT_SOCK[] = "/tmp/clientXXXXXX"; if(mkstemp(CLIENT_SOCK) < 0) { printf("[my_init_lib] Unable to create client socket.\n"); return -1; } if(snfs_init(CLIENT_SOCK, SERVER_SOCK) < 0) { printf("[my_init_lib] Unable to initialize SNFS API.\n"); return -1; } Open_files_list = queue_create(); Lib_initted = 1; return 0; }
int remove_value_test(){ queue *q = queue_create(); int x = 0, y = 1, z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); assert(queue_size(q) == 3); int *ret_val; queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == x); queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == y); queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == z); queue_destroy(q,false); return 0; }
thread_pool_t *thread_pool_create(unsigned int threads, int (*task_exec)(void *task)) { thread_pool_t *this_ptr = (thread_pool_t*) malloc(sizeof(thread_pool_t)); int i; if ((this_ptr != NULL) && (task_exec != NULL)) { this_ptr->task_exec = task_exec; this_ptr->threads = (pthread_t*) malloc(sizeof(pthread_t) * threads - 1); this_ptr->condititon = (pthread_cond_t*) malloc(sizeof(pthread_cond_t)); this_ptr->mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); this_ptr->queue = queue_create(); if ((this_ptr->queue != NULL) && (this_ptr->threads != NULL) && (this_ptr->condititon != NULL) && (this_ptr->mutex != NULL)) { this_ptr->continue_thread_pool = true; if (threads > 0) { this_ptr->thread_size = threads - 1; } else { this_ptr->thread_size = 0; } this_ptr->passive_threads = 0; this_ptr->tasks = 0; pthread_cond_init(this_ptr->condititon, NULL); pthread_mutex_init(this_ptr->mutex, NULL); for (i = 0; i < this_ptr->thread_size; i++) { pthread_create(&this_ptr->threads[i], NULL, thread_pool_run_thread, this_ptr); } } else { free(this_ptr->threads); free(this_ptr->condititon); free(this_ptr->mutex); queue_destroy(this_ptr->queue); free(this_ptr); this_ptr = NULL; } } return this_ptr; }
TCAS_Error_Code libtcas_double_cache_init(const char *filename, tcas_u32 fpsNumerator, tcas_u32 fpsDenominator, tcas_u16 width, tcas_u16 height, int maxFrameCount, int maxFrameChunkCount, int fileCacheSize, TCAS_pDoubleCache pDoubleCache) { TCAS_Error_Code error; error = libtcas_frame_chunks_cache_init(filename, fpsNumerator, fpsDenominator, width, height, maxFrameChunkCount, fileCacheSize, &pDoubleCache->dcpArgs.fcc); if (tcas_error_success != error) return error; pDoubleCache->dcpArgs.width = width; pDoubleCache->dcpArgs.height = height; pDoubleCache->minFrame = (tcas_u32)((tcas_u64)pDoubleCache->dcpArgs.fcc.fccpArgs.header.minTime * fpsNumerator / (fpsDenominator * 1000)) + 1; pDoubleCache->maxFrame = (tcas_u32)((tcas_u64)pDoubleCache->dcpArgs.fcc.fccpArgs.header.maxTime * fpsNumerator / (fpsDenominator * 1000)) + 1; pDoubleCache->maxFrameCount = maxFrameCount; queue_create(&pDoubleCache->qFrames, sizeof(TCAS_QueuedFrame), pDoubleCache->maxFrameCount, NULL, NULL); /* stores pointers to frame buffers */ pDoubleCache->semQueue = CreateSemaphore(NULL, 0, pDoubleCache->maxFrameCount, NULL); pDoubleCache->semFrames = CreateSemaphore(NULL, 0, pDoubleCache->maxFrameCount, NULL); InitializeCriticalSection(&pDoubleCache->lock); pDoubleCache->active = 1; pDoubleCache->tdWorker = CreateThread(NULL, 0, _libtcas_create_frame_with_chunks_cached_worker_proc, pDoubleCache, 0, &pDoubleCache->threadID); return tcas_error_success; }
/* Create queue of size jobs * id: [0:size-1] */ struct queue *make_test_queue (int size) { struct queue *q; flux_jobid_t id; q = queue_create (); if (!q) BAIL_OUT ("could not create queue"); for (id = 0; id < size; id++) { struct job *j; if (!(j = job_create (id, 0, 0, 0, 0))) BAIL_OUT ("job_create failed"); if (queue_insert (q, j) < 0) BAIL_OUT ("queue_insert failed"); } return q; }
int main(void) { t_logger *logger; t_queue *queue; int a; int *b; a = 5; queue = queue_create(); if (queue_enqueue(queue, &a) < 0) return (-1); b = (int *)queue_dequeue(queue); queue_destroy(queue); logger = logger_create(); logger_write("Ceci est une gestion d\'erreur.\n"); logger_destroy(); return (0); }
int main (int argc, char *argv[]) { int i; struct queue *queue; queue = queue_create(); if (queue == NULL) { return -1; } for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-") == 0) { printf("%s ", (char *) queue_dequeue(queue)); } else { queue_enqueue(queue, argv[i]); } } queue_destroy(queue); return 0; }
void crearNivel(t_list* lNiveles, tNivel* pNivelNuevo, int socket, char *nombreNivel, tInfoNivel *pInfoNivel) { pNivelNuevo->nombre = malloc(strlen(nombreNivel) + 1); strcpy(pNivelNuevo->nombre, nombreNivel); pNivelNuevo->cListos = queue_create(); pNivelNuevo->lBloqueados = list_create(); pNivelNuevo->socket = socket; pNivelNuevo->quantum = pInfoNivel->quantum; pNivelNuevo->algoritmo = pInfoNivel->algoritmo; pNivelNuevo->delay = pInfoNivel->delay; pNivelNuevo->maxSock = socket; pNivelNuevo->rdDefault = 999; pthread_cond_init(&(pNivelNuevo->hayPersonajes), NULL); FD_ZERO(&(pNivelNuevo->masterfds)); pthread_mutex_lock(&mtxlNiveles); list_add(lNiveles, pNivelNuevo); pthread_mutex_unlock(&mtxlNiveles); }
void gprs_init(void) { sim928a_gprs_init(); gprs_uart_init(); queue_create(&gprs_queue); // 初始化,构造空队 #if GPRS_TEST gprs_config_t gprs_test_config; #define BUILD_IP_ADDRESS(b3, b2, b1, b0) ((uint32_t)(b3) << 24) | \ ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | ((uint32_t)(b0)) gprs_test_config.ip_addr = BUILD_IP_ADDRESS(58, 214, 236, 152); gprs_test_config.port = 8066; gprs_test_config.type_cb = gprs_test_send_cb; gprs_test_config.data_cb = gprs_test_receive_cb; gprs_config(&gprs_test_config); uint8_t DATA[4] = {"1234"}; gprs_set_mode(CONTINE_MODE); gprs_send(DATA, 0x04, 1, 0); gprs_send(DATA, 0x04, 2, 0); gprs_send(DATA, 0x04, 3, 0); gprs_send(DATA, 0x04, 4, 0); gprs_send(DATA, 0x04, 4, 1000); gprs_send(DATA, 0x04, 4, 1000); gprs_send(DATA, 0x04, 4, 1000); #endif gprs_flush_receive_buf(); wsnos_init_printf(NULL, NULL); // 创建任务 osel_task_tcb *gprs_task_handle = osel_task_create(&gprs_task, GPRS_TASK_PRIO); // 消息绑定 osel_subscribe(gprs_task_handle, GPRS_SEND_EVENT); osel_subscribe(gprs_task_handle, GPRS_POLL_EVENT); osel_subscribe(gprs_task_handle, GPRS_SEND_CMD_EVENT); osel_subscribe(gprs_task_handle, GPRS_RESPONSE_EVENT); osel_post(GPRS_POLL_EVENT, NULL, OSEL_EVENT_PRIO_LOW); }
NetPollEvents *net_poll_events_create (void) { NetPollEvents *events; struct epoll_event event = { 0 }; if (!(events = memory_create (sizeof (NetPollEvents)))) { error_code (FunctionCall, 1); return NULL; } events->file = -1; events->internal_event = -1; events->event_index = 0; events->event_count = 0; events->queue_got_events = false; if (!queue_create (&events->queue, 1024, sizeof (NetPollEvent), QUEUE_SIZE_DYNAMIC)) { memory_destroy (events); error_code (FunctionCall, 2); return NULL; } if ((events->file = epoll_create1 (0)) == -1) { net_poll_events_destroy (events); error_code (SystemCall, 1); return NULL; } if ((events->internal_event = eventfd (0, EFD_NONBLOCK)) == -1) { error_code (SystemCall, 2); net_poll_events_destroy (events); return NULL; } event.data.ptr = &events->internal_event; event.events = EPOLLIN | EPOLLET; if (epoll_ctl (events->file, EPOLL_CTL_ADD, events->internal_event, &event) == -1) { net_poll_events_destroy (events); error_code (SystemCall, 3); return NULL; } return events; }
struct msgqueue * msgqueue_create( uint32_t size ) { struct msgqueue * self = NULL; self = (struct msgqueue *)malloc( sizeof(struct msgqueue) ); if ( self ) { self->popfd = -1; self->pushfd = -1; pthread_spin_init( &self->lock, 0 ); self->queue = queue_create( size ); if ( self->queue == NULL ) { msgqueue_destroy( self ); self = NULL; } else { int32_t rc = -1; int32_t fds[2] = { -1, -1 }; rc = socketpair( AF_UNIX, SOCK_STREAM, 0, fds ); if ( rc == -1 ) { msgqueue_destroy( self ); self = NULL; } else { self->popfd = fds[0]; self->pushfd = fds[1]; #ifdef O_NOATIME // linux在读pipe的时候会更新访问时间, touch_atime(), 这个的开销也不小 fcntl( self->popfd, F_SETFL, O_NOATIME ); #endif } } } return self; }
static void cmdBroadcast(void *base) { node_t *node = (node_t *) base; queue_t *q = NULL; message_t *msg; assert(node); assert(node->handle >= 0); assert(node->sysdata); logger(node->sysdata->logging, 3, "node:%d BROADCAST (flags:%x, mask:%x)", node->handle, node->data.flags, node->data.mask); // do we have a queue name, or a qid? if (BIT_TEST(node->data.mask, DATA_MASK_QUEUE) || BIT_TEST(node->data.mask, DATA_MASK_QUEUEID)) { if (BIT_TEST(node->data.mask, DATA_MASK_QUEUE)) { q = queue_get_name(node->sysdata->queues, expbuf_string(&node->data.queue)); } else if (BIT_TEST(node->data.mask, DATA_MASK_QUEUEID)) { q = queue_get_id(node->sysdata->queues, node->data.qid); } if (q == NULL) { q = queue_create(node->sysdata, expbuf_string(&node->data.queue)); } // by this point, we should have 'q'. assert(q); // create message object. msg = next_message(node); assert(msg); // now that we have a message structure completely filled out with the // data from the node, then we need to do something with it. assert(0); } else { // we didn't have a queue name, or a queue id. We need to handle this gracefully. assert(0); } }
struct fs_queue * fs_queue_create(size_t nr_blocks, size_t block_size) { struct buf * bp; struct fs_queue * fsq; bp = geteblk(sizeof(struct fs_queue) + nr_blocks * (sizeof(struct fs_queue_packet) + block_size)); if (!bp) return NULL; fsq = (struct fs_queue *)(bp->b_data); fsq->qcb = queue_create(fsq->packet, block_size, nr_blocks); mtx_init(&fsq->wr_lock, MTX_TYPE_TICKET, MTX_OPT_DEFAULT); mtx_init(&fsq->rd_lock, MTX_TYPE_TICKET, MTX_OPT_DEFAULT); fsq->bp = bp; return fsq; }
liberadosNoSerializadoStruct *desserializador_liberados(void *data,int16_t length) { liberadosNoSerializadoStruct *self = malloc(sizeof(liberadosNoSerializadoStruct)); recursoStruct *elem; int32_t i, offset = 0, tmp_size = 0; self->cola=queue_create(); int32_t tamanioCola=length; for (i = 0; i < tamanioCola; i++) { offset += tmp_size; elem = malloc(sizeof(recursoStruct)); memcpy(elem,data + offset, sizeof(recursoStruct)); queue_push(self->cola,elem); tmp_size=sizeof(recursoStruct); } return self; }
T_QUEUE ipc_setup(void) { /* Framework initializations */ T_QUEUE queue = queue_create(32, NULL); assert(queue); /* Initialize IPC: channels have been chosen arbitrarily */ ipc_init(0, 5, 1, 6, CPU_ID_ARC); ipc_async_init(queue); set_cpu_id(CPU_ID_QUARK); set_cpu_message_sender(CPU_ID_ARC, ipc_async_send_message); set_cpu_free_handler(CPU_ID_ARC, ipc_async_free_message); #ifdef CONFIG_IPC_UART set_cpu_message_sender(CPU_ID_BLE, send_message_ipc_uart); set_cpu_free_handler(CPU_ID_BLE, free_message_ipc_uart); #endif return queue; }
error_t module_timer (system_state_t _state) { usize_t idx; error_t ecode; static task_handle_t handle; if (STATE_UP == _state) { STACK_DECLARE (stack, CONFIG_TIMER_TASK_STACK_SIZE); QUEUE_BUFFER_DECLARE (buffer, sizeof (timer_handle_t), CONFIG_TIMER_QUEUE_SIZE); ecode = queue_create ("Timer", &g_timer_queue, buffer, sizeof (timer_handle_t), CONFIG_TIMER_QUEUE_SIZE); if (0 != ecode) { return ecode; } ecode = task_create (&handle, "Timer", CONFIG_TIMER_TASK_PRIORITY, stack, sizeof (stack)); if (0 != ecode) { return ecode; } ecode = task_start (handle, task_timer, g_timer_queue); if (0 != ecode) { return ecode; } } else if (STATE_DOWN == _state) { if (0 != task_delete (handle)) { console_print ("Error: cannot delete task \"Timer\""); } if (0 != queue_delete (g_timer_queue)) { console_print ("Error: cannot delete queue \"Timer\""); } } else if (STATE_DESTROYING == _state) { for (idx = 0; idx <= BUCKET_LAST_INDEX; ++ idx) { (void) dll_traverse (&g_buckets [idx].dll_, timer_check_for_each, 0); } (void) dll_traverse (&g_inactive_timer, timer_check_for_each, 0); } return 0; }