int thread_rwlock_init(rwlock_t *rwlock) { if (rwlock == NULL) return_errno(FALSE, EINVAL); rwlock->rw_state = THREAD_RWLOCK_INITIALIZED; rwlock->rw_readers = 0; thread_mutex_init(&rwlock->rw_mutex_rd, NULL); thread_mutex_init(&rwlock->rw_mutex_rw, NULL); return TRUE; }
HavokResponse *db_mysql_get_pc_list( HavokRequest *req) { MYSQL_BIND *data; pthread_mutex_t *mutex; HavokResponse *resp; if( !req || !req->account_data->id ) { return( NULL ); } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); data = CREATEN(MYSQL_BIND, 1); bind_numeric( &data[0], req->account_data->id, MYSQL_TYPE_LONG ); db_queue_query( 8, QueryTable, data, 1, result_get_pc_list, (void *)&resp, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); return( resp ); }
HavokResponse *db_mysql_load_pc( HavokRequest *req ) { MYSQL_BIND *data; pthread_mutex_t *mutex; HavokResponse *resp; if( !req || !req->pc_data->account_id || !req->pc_data->id ) { return( NULL ); } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); data = CREATEN(MYSQL_BIND, 2); bind_numeric( &data[0], req->pc_data->account_id, MYSQL_TYPE_LONG ); bind_numeric( &data[1], req->pc_data->id, MYSQL_TYPE_LONG ); db_queue_query( 9, QueryTable, data, 2, result_load_pc, (void *)&resp, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); resp->pc_data[0]->attribs = db_mysql_load_pc_attribs( req->pc_data->id ); return( resp ); }
HavokResponse *db_mysql_find_pc( HavokRequest *req ) { MYSQL_BIND *data; pthread_mutex_t *mutex; HavokResponse *resp; if( !req || !req->pc_data || !req->pc_data->name ) { return( NULL ); } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); data = CREATEN(MYSQL_BIND, 1); bind_string( &data[0], req->pc_data->name, MYSQL_TYPE_VAR_STRING ); db_queue_query( 17, QueryTable, data, 1, result_find_pc, (void *)&resp, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); return( resp ); }
int main(int argc, char **argv) { int result; thread_mutex_t mutex; era_arm_t arm; era_trajectory_t trajectory; if (era_init_arg(&arm, argc, argv, 0, "FILE FREQUENCY")) return -1; const char* file = argv[1]; float freq = atof(argv[2]); thread_mutex_init(&mutex); if ((result = era_trajectory_read(file, &trajectory)) < 0) { fprintf(stderr, "%s\n", era_trajectory_errors[-result]); return -1; } signal(SIGINT, era_signaled); if (era_open(&arm)) return -1; if (!(result = era_control_open_loop_start(&control_thread, &arm, &mutex, &trajectory, freq))) thread_wait_exit(&control_thread); else fprintf(stderr, "%s\n", era_control_open_loop_errors[result]); era_close(&arm); era_destroy(&arm); thread_mutex_destroy(&mutex); return 0; }
/** * \brief Initialize the pinned region * * Allocates a region of virtual address space and initializes its state. */ errval_t vspace_pinned_init(void) { errval_t err; struct pinned_state *state = get_current_pinned_state(); struct vspace *vspace = get_current_vspace(); err = memobj_create_pinned(&state->memobj, VSPACE_PINNED_SIZE, 0); if (err_is_fail(err)) { return err_push(err, LIB_ERR_MEMOBJ_CREATE_PINNED); } err = vregion_map(&state->vregion, vspace, (struct memobj*)&state->memobj, 0, VSPACE_PINNED_SIZE, VREGION_FLAGS_READ_WRITE); if (err_is_fail(err)) { return err_push(err, LIB_ERR_VREGION_MAP); } state->offset = 0; thread_mutex_init(&state->mutex); slab_init(&state->vregion_list_slab, VSPACE_PINNED_UNIT * sizeof(struct vregion_list), NULL); slab_init(&state->frame_list_slab, VSPACE_PINNED_UNIT * sizeof(struct memobj_frame_list), NULL); return SYS_ERR_OK; }
/** * \brief initializes and allocates a nested OpenMP lock * * \param arg returned pointer to the lock * * The effect of these routines is to initialize the lock to the unlocked state; * that is, no task owns the lock. In addition, the nesting count for a nestable * lock is set to zero. */ void omp_init_nest_lock(omp_nest_lock_t *arg) { struct __omp_nested_lock *nlock = arg; #ifdef BARRELFISH switch (g_bomp_state->backend_type) { case BOMP_BACKEND_BOMP: thread_mutex_init(&nlock->mutex); break; case BOMP_BACKEND_XOMP: assert("NYI"); break; default: USER_PANIC("Invalid Backend Type"); break; } #else nlock = calloc(1, sizeof(struct __omp_nested_lock)); if (lock == NULL) { printf("failed to allocate lock\n"); abort(); } pthread_mutex_init(&nlock->mutex, NULL); #endif nlock->owner = NULL; nlock->count = 0; nlock->initialized = 1; }
char *db_mysql_load_pc_attribs( int pc_id ) { MYSQL_BIND *data; pthread_mutex_t *mutex; char *resp; if( !pc_id ) { return( NULL ); } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); data = CREATEN(MYSQL_BIND, 1); bind_numeric( &data[0], pc_id, MYSQL_TYPE_LONG ); db_queue_query( 13, QueryTable, data, 1, result_load_pc_attribs, (void *)&resp, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); return( resp ); }
HavokResponse *db_mysql_save_account( HavokRequest *req ) { MYSQL_BIND *data; pthread_mutex_t *mutex; HavokResponse *resp; ProtectedData_t *protect; volatile int id; if( !req || !req->account_data ) { return; } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); protect = ProtectedDataCreate(); protect->data = (void *)&id; ProtectedDataLock( protect ); data = CREATEN(MYSQL_BIND, 7); bind_numeric( &data[0], req->account_data->id, MYSQL_TYPE_LONG ); bind_string( &data[1], req->account_data->email, MYSQL_TYPE_VAR_STRING ); bind_string( &data[2], req->account_data->passwd, MYSQL_TYPE_VAR_STRING ); bind_numeric( &data[3], (req->account_data->ansi ? 1 : 0), MYSQL_TYPE_TINY ); bind_numeric( &data[4], (req->account_data->confirmed ? 1 : 0), MYSQL_TYPE_TINY ); bind_string( &data[5], (req->account_data->confcode ? req->account_data->confcode : ""), MYSQL_TYPE_VAR_STRING ); bind_null_blob( &data[6], protect ); db_queue_query( 5, QueryTable, data, 7, NULL, NULL, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); ProtectedDataLock( protect ); ProtectedDataDestroy( protect ); resp = protobufCreateResponse(); if( !resp ) { return( NULL ); } resp->request_type = REQ_TYPE__SAVE_ACCOUNT; resp->account_data = CREATE(ReqAccountType); req_account_type__init( resp->account_data ); memcpy( resp->account_data, req->account_data, sizeof(ReqAccountType) ); resp->account_data->id = id; resp->account_data->email = memstrlink( req->account_data->email ); resp->account_data->passwd = memstrlink( req->account_data->passwd ); resp->account_data->confcode = memstrlink( req->account_data->confcode ); return( resp ); }
/** * \brief Initialise a new event queue * * \param q Storage for event queue * \param waitset Waitset that will service the queue * \param mode Operating mode for the queue */ void event_queue_init(struct event_queue *q, struct waitset *waitset, enum event_queue_mode mode) { waitset_chanstate_init(&q->waitset_state, CHANTYPE_EVENT_QUEUE); thread_mutex_init(&q->mutex); q->head = q->tail = NULL; q->waitset = waitset; q->mode = mode; }
static mr_lock_t ptmutex_alloc(void) { struct thread_mutex *m; m = malloc(sizeof(struct thread_mutex)); assert (m != NULL); thread_mutex_init(m); return m; }
HavokResponse *db_mysql_save_pc( HavokRequest *req ) { MYSQL_BIND *data; pthread_mutex_t *mutex; HavokResponse *resp; ProtectedData_t *protect; volatile int id; if( !req || !req->pc_data ) { return( NULL ); } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); protect = ProtectedDataCreate(); protect->data = (void *)&id; ProtectedDataLock( protect ); data = CREATEN(MYSQL_BIND, 4); bind_numeric( &data[0], req->pc_data->id, MYSQL_TYPE_LONG ); bind_numeric( &data[1], req->pc_data->account_id, MYSQL_TYPE_LONG ); bind_string( &data[2], req->pc_data->name, MYSQL_TYPE_VAR_STRING ); bind_null_blob( &data[3], protect ); db_queue_query( 10, QueryTable, data, 4, NULL, NULL, mutex ); pthread_mutex_unlock( mutex ); pthread_mutex_destroy( mutex ); memfree( mutex ); ProtectedDataLock( protect ); ProtectedDataDestroy( protect ); db_mysql_save_pc_attribs( id, req->pc_data->attribs ); resp = protobufCreateResponse(); if( !resp ) { return( NULL ); } resp->request_type = REQ_TYPE__SAVE_PC; resp->n_pc_data = 1; resp->pc_data = CREATE(ReqPCType *); resp->pc_data[0] = CREATE(ReqPCType); req_pctype__init( resp->pc_data[0] ); memcpy( resp->pc_data[0], req->pc_data, sizeof(ReqPCType) ); resp->pc_data[0]->id = id; resp->pc_data[0]->name = memstrlink( req->pc_data->name ); return( resp ); }
void init_jobs(void) { jobs_free=0; jobs_size=100; job=do_alloc(jobs_size, sizeof(JOB)); jobs_submitted=0; jobs_done=0; next_job_to_do=0; thread_mutex_init(&jobs_mutex); thread_cond_init(&wait_for_more_jobs_condition); thread_cond_init(&all_done_condition); }
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { mutex_t *m; (void) attr; pthread_initialize(); if (mutex == NULL) return_errno(EINVAL, EINVAL); if ((m = (mutex_t *)malloc(sizeof(mutex_t))) == NULL) return errno; if (!thread_mutex_init(m, "pthread_mutex")) return errno; (*mutex) = (pthread_mutex_t)m; return OK; }
void init_threads(int mt) { max_threads=mt-1; if(max_threads<0) { max_threads=cpu_count()-1; } if(max_threads<0) max_threads=0; thread_id=do_alloc(max_threads, sizeof(*thread_id)); num_threads=0; threads_started=0; thread_cond_init(&thread_not_needed); thread_mutex_init(&thread_num_mutex); set_concurrency(1); fprintf(stderr, "maximum threads: %d\n", max_threads+1); fprintf(LOG, "maximum threads: %d\n", max_threads+1); }
errval_t morecore_init(void) { errval_t err; struct morecore_state *state = get_morecore_state(); thread_mutex_init(&state->mutex); err = vspace_mmu_aware_init(&state->mmu_state, HEAP_REGION); if (err_is_fail(err)) { return err_push(err, LIB_ERR_VSPACE_MMU_AWARE_INIT); } sys_morecore_alloc = morecore_alloc; sys_morecore_free = morecore_free; return SYS_ERR_OK; }
void thread_create( pthread_t *pthreadId, void * (*routine)(void *), void *arg, char *name, ThreadCallback_t *callbacks ) { pthread_attr_t attr; if( !startupMutex ) { startupMutex = CREATE(pthread_mutex_t); thread_mutex_init( startupMutex ); } pthread_mutex_lock( startupMutex ); pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, DEFAULT_STACK_SIZE); pthread_create( pthreadId, &attr, routine, arg ); pthread_attr_destroy(&attr); thread_register( pthreadId, name, callbacks ); pthread_mutex_unlock( startupMutex ); }
/** * \brief initializes and allocates a simple OpenMP lock * * \param arg returned pointer to the lock * * The effect of these routines is to initialize the lock to the unlocked state; * that is, no task owns the lock. */ void omp_init_lock(omp_lock_t *lock) { #ifdef BARRELFISH switch (g_bomp_state->backend_type) { case BOMP_BACKEND_BOMP: thread_mutex_init(&lock->mutex); break; case BOMP_BACKEND_XOMP: assert("NYI"); break; default: USER_PANIC("Invalid Backend Type"); break; } #else pthread_mutex_init(&lock->mutex, NULL); #endif lock->initialized = 0x1; }
EXTREME_INFO * allocate_extreme_info(char *name) { EXTREME_INFO *ei; int i; ei=do_alloc(1, sizeof(*ei)); memset(ei, 0, sizeof(*ei)); ei->name=strdup(name); thread_mutex_init(&(ei->mutex)); if(args_info.compute_skymaps_arg) { ei->ul_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->ul_freq_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->circ_ul_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->circ_ul_freq_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->snr_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->snr_ul_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->snr_freq_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->max_weight_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->min_weight_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->weight_loss_fraction_skymap=do_alloc(patch_grid->npoints, sizeof(float)); ei->ks_skymap=do_alloc(patch_grid->npoints, sizeof(float)); } ei->band_info=do_alloc(fine_grid->nbands, sizeof(*ei->band_info)); memset(ei->band_info, 0, fine_grid->nbands*sizeof(*ei->band_info)); ei->band_valid_count=do_alloc(fine_grid->nbands, sizeof(*ei->band_valid_count)); ei->band_masked_count=do_alloc(fine_grid->nbands, sizeof(*ei->band_masked_count)); memset(ei->band_valid_count, 0, fine_grid->nbands*sizeof(*ei->band_valid_count)); memset(ei->band_masked_count, 0, fine_grid->nbands*sizeof(*ei->band_masked_count)); for(i=0;i<fine_grid->nbands;i++) { ei->band_info[i].max_weight=-1; } return ei; }
void db_mysql_save_pc_attribs( int pc_id, char *json ) { MYSQL_BIND *data; pthread_mutex_t *mutex; JSONSource_t *js; JSONSource_t *jsItem; if( !pc_id || !json ) { return; } js = SplitJSON( json ); if( !js ) { return; } mutex = CREATE(pthread_mutex_t); thread_mutex_init( mutex ); for( jsItem = js; jsItem->source; jsItem++ ) { data = CREATEN(MYSQL_BIND, 3); bind_numeric( &data[0], pc_id, MYSQL_TYPE_LONG ); bind_string( &data[1], jsItem->source, MYSQL_TYPE_VAR_STRING ); bind_string( &data[2], jsItem->json, MYSQL_TYPE_VAR_STRING ); db_queue_query( 14, QueryTable, data, 3, NULL, NULL, mutex ); pthread_mutex_unlock( mutex ); } pthread_mutex_destroy( mutex ); memfree( mutex ); DestroyJSONSource( js ); }
void StartThreads( void ) { pthread_mutex_t spinLockMutex; pid_t childPid; struct sigaction sa; sigset_t sigmsk; size_t len; ThreadCallback_t callbacks; GlobalAbort = FALSE; #ifndef __CYGWIN__ len = confstr( _CS_GNU_LIBPTHREAD_VERSION, NULL, 0 ); if( len ) { pthreadsVersion = CREATEN(char, len); confstr( _CS_GNU_LIBPTHREAD_VERSION, pthreadsVersion, len ); } #else (void)len; pthreadsVersion = memstrlink( "Cygwin" ); #endif if( !pthreadsVersion || strstr( pthreadsVersion, "linuxthreads" ) ) { fprintf( stderr, "havokmud requires NPTL to operate correctly.\n\n" "The signal handling in linuxthreads is just too " "broken to use.\n\n" ); exit( 1 ); } /* Do we need to detach? */ if( Daemon ) { childPid = fork(); if( childPid < 0 ) { perror( "Couldn't detach in daemon mode" ); _exit( 1 ); } if( childPid != 0 ) { /* This is still the parent, report the child's pid and exit */ printf( "[Detached as PID %d]\n", childPid ); /* And exit the parent */ _exit( 0 ); } /* After this is in the detached child */ /* Close stdin, stdout, stderr to release the tty */ close(0); close(1); close(2); } LoggingQ = QueueCreate( 1024 ); ConnectInputQ = QueueCreate( 256 ); ConnectDnsQ = QueueCreate( 64 ); InputLoginQ = QueueCreate( 256 ); InputEditorQ = QueueCreate( 256 ); InputPlayerQ = QueueCreate( 256 ); InputImmortQ = QueueCreate( 256 ); MailQ = QueueCreate( 128 ); QueryQ = QueueCreate( 1024 ); ProtobufQ = QueueCreate( 1024 ); mainThreadId = pthread_self(); /* * Setup the sigmasks for this thread (which is the parent to all others). * This will propogate to all children. */ sigfillset( &sigmsk ); sigdelset( &sigmsk, SIGUSR1 ); sigdelset( &sigmsk, SIGUSR2 ); sigdelset( &sigmsk, SIGHUP ); sigdelset( &sigmsk, SIGWINCH ); sigdelset( &sigmsk, SIGINT ); sigdelset( &sigmsk, SIGSEGV ); sigdelset( &sigmsk, SIGILL ); sigdelset( &sigmsk, SIGFPE ); pthread_sigmask( SIG_SETMASK, &sigmsk, NULL ); memset( &callbacks, 0, sizeof(ThreadCallback_t) ); callbacks.sighupFunc = mainSighup; thread_register( &mainThreadId, "MainThread", NULL ); /* Setup signal handler for SIGUSR1 (toggles Debug) */ sa.sa_sigaction = (sigAction_t)logging_toggle_debug; sigemptyset( &sa.sa_mask ); sa.sa_flags = SA_RESTART; sigaction( SIGUSR1, &sa, NULL ); /* Setup the exit handler */ atexit( MainDelayExit ); /* Setup signal handler for SIGINT (shut down cleanly) */ sa.sa_sigaction = (sigAction_t)signal_interrupt; sigemptyset( &sa.sa_mask ); sa.sa_flags = SA_RESTART; sigaction( SIGINT, &sa, NULL ); /* Setup signal handlers that are to be propogated to all threads */ sa.sa_sigaction = (sigAction_t)signal_everyone; sigemptyset( &sa.sa_mask ); sa.sa_flags = SA_RESTART | SA_SIGINFO; sigaction( SIGUSR2, &sa, NULL ); sigaction( SIGHUP, &sa, NULL ); sigaction( SIGWINCH, &sa, NULL ); /* Setup signal handlers for SEGV, ILL, FPE */ sa.sa_sigaction = (sigAction_t)signal_death; sigemptyset( &sa.sa_mask ); sa.sa_flags = SA_RESTART | SA_SIGINFO; sigaction( SIGSEGV, &sa, NULL ); sigaction( SIGILL, &sa, NULL ); sigaction( SIGFPE, &sa, NULL ); versionAdd( "pthreads", pthreadsVersion ); versionAdd( "TERM", getenv("TERM") ); thread_create( &loggingThreadId, LoggingThread, NULL, "LoggingThread", NULL ); #if 0 curses_start(); cursesMenuItemAdd( 2, MENU_SYSTEM, "About", mainAbout, NULL ); cursesMenuItemAdd( 2, MENU_SYSTEM, "Licensing", mainLicensing, NULL ); cursesMenuItemAdd( 2, MENU_SYSTEM, "Versions", mainVersions, NULL ); cursesMenuItemAdd( 2, MENU_SYSTEM, "Reload All", mainReloadAll, NULL ); #endif LogBanner(); db_init(); db_mysql_init(); memset( &callbacks, 0, sizeof(ThreadCallback_t) ); callbacks.sigusr2Func = memoryStats; thread_create( &memoryThreadId, MemoryCoalesceThread, NULL, "MemoryCoalesceThread", &callbacks ); thread_create( &dnsThreadId, DnsThread, NULL, "DnsThread", NULL ); thread_create( &inputThreadId, InputThread, NULL, "InputThread", NULL ); thread_create( &loginThreadId, LoginThread, NULL, "LoginThread", NULL ); thread_create( &editorThreadId, EditorThread, NULL, "EditorThread", NULL ); mortalPlayingArgs.inputQ = InputPlayerQ; thread_create( &mortalPlayingThreadId, PlayingThread, &mortalPlayingArgs, "MortalPlayingThread", NULL ); immortPlayingArgs.inputQ = InputImmortQ; thread_create( &immortPlayingThreadId, PlayingThread, &immortPlayingArgs, "ImmortPlayingThread", NULL ); thread_create( &mysqlThreadId, MysqlThread, NULL, "MySQLThread", NULL ); thread_create( &protobufThreadId, ProtobufThread, NULL, "ProtobufThread", NULL ); pthread_mutex_lock( startupMutex ); pthread_mutex_unlock( startupMutex ); db_check_schema_main(); thread_create( &smtpThreadId, SmtpThread, NULL, "SMTPThread", NULL ); connectThreadArgs.port = mud_port; connectThreadArgs.timeout_sec = 0; connectThreadArgs.timeout_usec = 100000; thread_create( &connectionThreadId, ConnectionThread, &connectThreadArgs, "ConnectionThread", NULL ); pthread_mutex_lock( startupMutex ); pthread_mutex_unlock( startupMutex ); /* Sit on this and rotate - this causes an intentional deadlock, this * thread should stop dead in its tracks */ thread_mutex_init( &spinLockMutex ); pthread_mutex_lock( &spinLockMutex ); pthread_mutex_lock( &spinLockMutex ); }
/** * \brief Initializer that does not allocate any space * * #slot_alloc_init duplicates some of the code below, * modify it if making changes here. * * XXX: top_buf head_buf and reserve_buf each point to a separate buffer of * size bufsize bytes which can be used for backing storage. bufsize evidently * needs to be >= sizeof(struct cnode_meta) * nslots / 2. Don't ask me why! -AB */ errval_t multi_slot_alloc_init_raw(struct multi_slot_allocator *ret, cslot_t nslots, struct capref top_cap, struct cnoderef top_cnode, void *top_buf, void *head_buf, void *reserve_buf, size_t bufsize) { errval_t err; struct capref cap; struct cnoderef cnode; /* Generic part */ ret->a.alloc = multi_alloc; ret->a.free = multi_free; ret->a.space = nslots; ret->a.nslots = nslots; thread_mutex_init(&ret->a.mutex); ret->head->next = NULL; ret->reserve->next = NULL; /* Top */ err = single_slot_alloc_init_raw((struct single_slot_allocator*)ret->top, top_cap, top_cnode, nslots, top_buf, bufsize); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SINGLE_SLOT_ALLOC_INIT); } /* Head */ err = ret->top->alloc(ret->top, &cap); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SLOT_ALLOC); } err = cnode_create_raw(cap, &cnode, nslots, NULL); if (err_is_fail(err)) { return err_push(err, LIB_ERR_CNODE_CREATE); } err = single_slot_alloc_init_raw(&ret->head->a, cap, cnode, nslots, head_buf, bufsize); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SINGLE_SLOT_ALLOC_INIT); } /* Reserve */ err = ret->top->alloc(ret->top, &cap); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SLOT_ALLOC); } err = cnode_create_raw(cap, &cnode, nslots, NULL); if (err_is_fail(err)) { return err_push(err, LIB_ERR_CNODE_CREATE); } err = single_slot_alloc_init_raw(&ret->reserve->a, cap, cnode, nslots, reserve_buf, bufsize); if (err_is_fail(err)) { return err_push(err, LIB_ERR_SINGLE_SLOT_ALLOC_INIT); } /* Slab */ size_t allocation_unit = sizeof(struct slot_allocator_list) + SINGLE_SLOT_ALLOC_BUFLEN(nslots); slab_init(&ret->slab, allocation_unit, NULL); return SYS_ERR_OK; }
int main (int argc, char *argv[]) { long i; int j; int correctSum = 0; int trials = TRIALS; int passed = 0; char *chkArg; if (argc != 2) { fprintf(stderr, "Usage: sync numberOfThreads\n"); exit(-1); } n = strtol(argv[1], &chkArg, 10); // validate the command-line argument // errno would indicate overflow, // null string is also not allowed of course, // and, the full argument must be consumed if (errno || (*argv[1] == '\0') || (*chkArg != '\0')) { fprintf(stderr, "invalid number of threads\n"); exit(-1); } // finally, given integer must be in the correct range if (n <= 0) { fprintf(stderr, "number of threads must be > 0\n"); exit(-1); } printf("using %d child threads\n", n); // compute the correct sum for (i = 0; i < N; i++) { correctSum += i; } // distribute the work chunk = N / n; split = N % n; if (split == 0) { split = n; chunk -= 1; } // initialize the mutex if (thread_mutex_init(&mu) == 0) error("can't init mutex"); if (thread_mutex_init(&mu2) == 0) error("can't init mutex"); // do the trials for (j = 0; j < trials; j++) { fprintf(stderr, "Trial %d/%d complete.\n", j, trials); cnt = 0; // number of child threads that have exited sum = 0; for (i=0; i < n; i++) { if(i%2 == 0) { // create threads; DANGER: thread logical id (int) passed as "void *" if (thread_create(work, (void *) i) == 0) error("error in thread create"); } else { // create threads; DANGER: thread logical id (int) passed as "void *" if (thread_create(work2, (void *) i) == 0) error("error in thread create"); } } // wait for all children to finish while (cnt != n) { thread_yield(); } if (sum == correctSum) { passed += 1; } } printf ("%d of %d trials passed\n", passed, trials); return 0; }