int sipc_shm_create(sipc_t *sipc) { if (!sipc) goto err; /* Create the shm segment */ sipc->s.shmid = shmget(sipc->key, sipc->data_size, SHM_CPERMS); if (sipc->s.shmid < 0) { sipc_error(sipc, "shmget: %s\n", strerror(errno)); goto err; } /* Create the side channel */ sipc->s.msqid = mqueue_create(sipc->key); if (sipc->s.shmid < 0) goto err; /* Set capacity of side channel to only 1 message */ if (mqueue_set_capacity(sipc->s.msqid, 1) < 0) goto err; return 0; err: if (sipc->s.shmid != (key_t) - 1) shmctl(sipc->s.shmid, IPC_RMID, NULL); return -1; }
s32 RegisterDevices( void ) { QueueID = mqueue_create(QueueSpace, 8); // s32 ret = device_register("/dev/flash", QueueID); //#ifdef DEBUG // dbgprintf("FFS:DeviceRegister(\"/dev/flash\"):%d\n", ret ); //#endif // if( ret < 0 ) // return ret; // // ret = device_register("/dev/boot2", QueueID); //#ifdef DEBUG // dbgprintf("FFS:DeviceRegister(\"/dev/boot2\"):%d\n", ret ); //#endif // if( ret < 0 ) // return ret; s32 ret = device_register("/", QueueID); #ifdef DEBUG //dbgprintf("FFS:DeviceRegister(\"/\"):%d\n", ret ); #endif if( ret < 0 ) return ret; // ret = device_register("/dev/sdio", QueueID ); //#ifdef DEBUG // dbgprintf("FFS:DeviceRegister(\"/dev/sdio\"):%d QueueID:%d\n", ret, QueueID ); //#endif return QueueID; }
void mqueue_init(MQueue q) { pthread_once(&mqueue_once, mqueue_once_init); if(pthread_getspecific(mqueue_key) != NULL) return; if(q == NULL) q = mqueue_create(); pthread_setspecific(mqueue_key, q); }
int main(int argc, char **argv) { pthread_t producer; pthread_t consumers[2]; mqueue *events = mqueue_create(); pthread_create(&producer, NULL, producerThread, (void *)events); pthread_create(&consumers[0], NULL, consumerThread, (void *)events); pthread_create(&consumers[1], NULL, consumerThread, (void *)events); pthread_join(producer, NULL); pthread_join(consumers[0], NULL); pthread_join(consumers[1], NULL); return 0; }
struct mqueuebatch *mqueuebatch_create(size_t nmemb, size_t size) { size_t i = 0; size_t qnum = get_cpunum(); struct mqueuebatch *qs = calloc(1, sizeof(*qs) + sizeof(struct mqueue *) * qnum); if (qs == NULL) return NULL; assert(qnum > 0); nmemb = (nmemb + qnum - 1) / qnum; qs->qnum = qnum; for (i = 0; i < qnum; i++) { if ((qs->queues[i] = mqueue_create(nmemb, size)) == NULL) { mqueuebatch_destroy(qs); return NULL; } } return qs; }
void udelay(int us) { u8 heap[0x10]; u32 msg; s32 mqueue = -1; s32 timer = -1; mqueue = mqueue_create(heap, 1); if(mqueue < 0) goto out; timer = TimerCreate(us, 0, mqueue, 0xbabababa); if(timer < 0) goto out; mqueue_recv(mqueue, &msg, 0); out: if(timer > 0) TimerDestroy(timer); if(mqueue > 0) mqueue_destroy(mqueue); }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Usage: %s: <ip> <port>\n", argv[0]); exit(1); } initscr(); nonl(); cbreak(); noecho(); keypad(stdscr, TRUE); if (has_colors()) { start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_YELLOW, COLOR_BLACK); init_pair(4, COLOR_BLUE, COLOR_BLACK); init_pair(5, COLOR_CYAN, COLOR_BLACK); init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(7, COLOR_WHITE, COLOR_BLACK); } int h, w; getmaxyx(stdscr, h, w); dispW = newwin(h - 2, w, 0, 0); inputW = newwin(2, w, h - 2, 0); nodelay(inputW, TRUE); scrollok(dispW, TRUE); sock = joinServer(argv[1], atoi(argv[2])); signal(SIGINT, cleanup); signal(SIGSEGV, cleanup); mvwaddch(inputW, 0, 0, ACS_ULCORNER); for (int i = 1; i < w; i++) { mvwaddch(inputW, 0, i, ACS_HLINE); } mvwaddch(inputW, 1, 0, ACS_VLINE); wrefresh(inputW); int pos = 0; char command[256]; memset(command, 0, 256); events = mqueue_create(); dispEvents = mqueue_create(); // Create event thread pthread_t networkThreadID; thread_args *args = malloc(sizeof(thread_args)); if (args == NULL) { fprintf(stderr, "Error allocating memory\n"); exit(1); } args->sock = sock; args->events = events; args->dispEvents = dispEvents; if (pthread_create(&networkThreadID, NULL, networkThread, (void *)args)) { fprintf(stderr, "Error creating thread\n"); exit(1); } pthread_t eventThreadID; if (pthread_create(&eventThreadID, NULL, eventThread, (void *)args)) { fprintf(stderr, "Error creating thread\n"); exit(1); } free(args); sendMessage(sock, "SYN"); while (1) { //char c = mvwgetch(inputW, 1, pos + 1); char c = wgetch(inputW); if (c == ERR) { // Run through display events while (!mqueue_is_empty(dispEvents)) { char *e = mqueue_dequeue(dispEvents); logMessage(e, CLIENT); free(e); } } else { // Check enter if (c == 13) { for (int i = 1; i <= pos; i++) { mvwaddch(inputW, 1, i, ' '); } command[pos] = 0; pos = 0; wmove(inputW, 1, 1); if (strlen(command) > 0) { msg_command *mc = malloc(sizeof(msg_command)); if (mc == NULL) { fprintf(stderr, "Error allocating memory\n"); exit(1); } mqueue_enqueue(events, (void *)mc, sizeof(msg_command)); } } else if (c == 127) { // Check backspace if (pos > 0) { --pos; mvwaddch(inputW, 1, pos + 2, ' '); command[pos] = 0; } } else { mvwaddch(inputW, 1, pos + 1, c); command[pos] = c; ++pos; } } } cleanup(0); return 0; }
static void tea_thread_new(int tid, TEA_OBJECT *to, SNODE *command) { THREAD_WORK *tw = NULL; TEA_OBJCFG *ocline; HASH_ITER hi; HASH_NODE *hn; int fatal = 0; DBG("[tea] Creating thread %d.", tid); if((tw = calloc(1, sizeof(THREAD_WORK))) == NULL) FAT("Cannot alloc THREAD_WORK struct for thread %d.", tid); tw->id = tid; tw->pthread_id = 0; tw->to = to; tw->options = hash_copy(command->command.thc.to->options); /* check methods */ if(tw->to->listener || tw->to->sender) tw->mqueue = mqueue_create(); pthreadex_flag_init(&(tw->mwaiting), 0); pthreadex_flag_name(&(tw->mwaiting), "mwaiting"); pthreadex_flag_init(&(tw->cleanup_do), 0); pthreadex_flag_name(&(tw->cleanup_do), "cleanup_do"); pthreadex_flag_init(&(tw->cleanup_done), 0); pthreadex_flag_name(&(tw->cleanup_done), "cleanup_done"); /* global thread initialization here */ if(tw->to->global_init && !tw->to->initialized) { tw->to->initialized = -1; tw->to->global_init(); } /* make space for thread data */ if((tw->data = calloc(1, to->datasize)) == NULL) FAT("%d:%s: No memory for thread data.", command->line, to->name); /* check config params */ if(to->cparams) { /* check only allowed params are defined */ for(hn = hash_iter_first(&hi, command->command.thc.to->options); !hash_iter_finished(&hi); hn = hash_iter_next(&hi)) { /* iterate all allowed params and stop if (1) declared */ /* parameter hn->key is found in allowed paramter list, */ /* or (2) stop if there is not moar allowed params */ for(ocline = to->cparams; ocline->name && strcasecmp(hn->key, ocline->name); ocline++) ; if(!ocline->name) FAT("%d:%s: Parameter %s not allowed here.", command->line, to->name, hn->key); } /* set params (apply configuration) */ for(ocline = to->cparams; ocline->name; ocline++) { SNODE *val; /* get configured value (or set default value if not specified) */ val = tea_thread_param_value_get(tw->options, ocline->name); /* check if parameter is optional */ if(!val && ocline->needed) FAT("%d:%s: Parameter %s is mandatory.", command->line, to->name, ocline->name); /* set value */ if(tea_thread_param_value_set(tw, ocline, val)) FAT("%d:%s: Cannot set parameter %s.", command->line, to->name, ocline->name); } } else { if(command->command.thc.to->options || command->command.thc.to->options->nentries > 0) FAT("%d:%s: Parameters not allowed for this type of thread.", command->line, to->name); } /* once configuration applied, launch thread configuration routine */ if(tw->to->configure && tw->to->configure(tw, command, 1)) FAT("%d:%s: Thread configuration failed.", command->line, to->name); /* add thread to the list */ pthreadex_lock_get_exclusive_n(&ttable_lock, "install-thread"); DBG("[tea] Installing thread %d.", tid); /* build threads */ if(ttable[tid]) FATAL_ERROR("Thread slot %d is used already.", tid); ttable[tid] = tw; /* launch thread */ if(pthread_create(&(tw->pthread_id), NULL, tea_thread, tw) != 0) { ERR_ERRNO("Error creating thread %d", tid); FATAL_ERROR("Fatal error happened during thread creation."); } termination: if(fatal && tw) { if(ttable[tid] == tw) ttable[tid] = NULL; if(tw->mqueue) mqueue_destroy(tw->mqueue); pthreadex_flag_destroy(&(tw->mwaiting)); pthreadex_flag_destroy(&(tw->cleanup_do)); pthreadex_flag_destroy(&(tw->cleanup_done)); free(tw); } pthreadex_lock_release(); if(!fatal) return; FAT("Aborting."); }