/* Initializes the minisocket layer. */ void minisocket_initialize() { int i; sock_array = (minisocket_t*)malloc(sizeof(struct minisocket)*NUM_SOCKETS); if (!sock_array) { return; } for (i = 0; i < NUM_SOCKETS; i++) { sock_array[i] = NULL; } client_lock = semaphore_create(); if (!client_lock) { free(sock_array); return; } server_lock = semaphore_create(); if (!server_lock) { free(sock_array); semaphore_destroy(client_lock); return; } semaphore_initialize(client_lock, 1); semaphore_initialize(server_lock, 1); network_get_my_address(my_addr); curr_client_idx = CLIENT_START; //printf("minisocket_initialize complete\n"); }
void vfs_init(void) { int i; vfs_table.sem = semaphore_create(1); openfile_table.sem = semaphore_create(1); KERNEL_ASSERT(vfs_table.sem != NULL && openfile_table.sem != NULL); /* Clear table of mounted filesystems. */ for(i=0; i<CONFIG_MAX_FILESYSTEMS; i++) { vfs_table.filesystems[i].filesystem = NULL; } /* Clear table of open files. */ for (i = 0; i < CONFIG_MAX_OPEN_FILES; i++) { openfile_table.files[i].filesystem = NULL; } vfs_op_sem = semaphore_create(1); vfs_unmount_sem = semaphore_create(0); vfs_ops = 0; vfs_usable = 1; kprintf("VFS: Max filesystems: %d, Max open files: %d\n", CONFIG_MAX_FILESYSTEMS, CONFIG_MAX_OPEN_FILES); }
BLOCKDEVICEP blockdevice_create(char *name, int blocksize, int numblocks) { BLOCKDEVICEP devp ; int rc ; if (blocksize != REAL_BLOCK_LENGTH) { /* ** For now we simplify things and just force ** all blockdevices to have 1k block sizes. ** We can fix this assumption up later. */ return NULL ; } if (strlen(name) >= BLOCK_DEV_MAX_NAME_LEN) { return NULL ; } devp = malloc(sizeof(BLOCKDEVICE)) ; if (NULL == devp) { return NULL ; } devp->name = malloc(strlen(name)+1) ; if (NULL == devp->name) { free(devp) ; return NULL ; } devp->sem = semaphore_create(1234, 0) ; devp->dirtysem = semaphore_create(1235, 0) ; /*if (! devp->sem) { free(devp) ; return NULL ; }*/ strcpy(devp->name, name) ; devp->blocksize = blocksize ; devp->numblocks = numblocks ; devp->lastblockchecked = 0 ; rc = shmem_open() ; if (0 == rc) { semaphore_destroy(devp->sem, 1) ; free(devp->name) ; free(devp) ; return NULL ; } devp->bd = (struct SHMEM_BLOCK_OVERLAY *) shmem_get_memptr() ; initialize_block_dev_shmem(devp->bd, blocksize, numblocks, name) ; return devp ; }
/* Performs any initialization of the miniroute layer, if required. */ void miniroute_initialize() { network_get_my_address(hostaddr); discovery_alarm = -1; intrpt_buffer = queue_new(); intrpt_sig = semaphore_create(); discovery_mutex = semaphore_create(); discovery_sig = semaphore_create(); route_cache = miniroute_cache_new(65536, SIZE_OF_ROUTE_CACHE, MINIROUTE_CACHED_ROUTE_EXPIRE); disc_cache = miniroute_cache_new(65536, SIZE_OF_ROUTE_CACHE, MINIROUTE_CACHED_ROUTE_EXPIRE * 10); if (NULL == intrpt_buffer || NULL == intrpt_sig || NULL == route_cache || NULL == discovery_mutex || NULL == discovery_sig) { queue_free(intrpt_buffer); semaphore_destroy(intrpt_sig); semaphore_destroy(discovery_mutex); semaphore_destroy(discovery_sig); miniroute_cache_destroy(route_cache); return; } semaphore_initialize(intrpt_sig, 0); semaphore_initialize(discovery_mutex, 1); semaphore_initialize(discovery_sig, 0); network_get_my_address(hostaddr); minithread_fork(miniroute_control, NULL); }
/* * Initialization. * * minithread_system_initialize: * This procedure should be called from your C main procedure * to turn a single threaded UNIX process into a multithreaded * program. * * Initialize any private data structures. * Create the idle thread. * Fork the thread which should call mainproc(mainarg) * Start scheduling. * */ void minithread_system_initialize(proc_t mainproc, arg_t mainarg) { minithread_t clean_up_thread = NULL; int a = 0; void* dummy_ptr = NULL; minithread_t tmp = NULL; tmp = NULL; dummy_ptr = (void*)&a; current_id = 0; // the next thread id to be assigned id_lock = semaphore_create(); semaphore_initialize(id_lock,1); runnable_q = multilevel_queue_new(4); blocked_q = queue_new(); blocked_q_lock = semaphore_create(); semaphore_initialize(blocked_q_lock,1); dead_q = queue_new(); dead_q_lock = semaphore_create(); semaphore_initialize(dead_q_lock,1); dead_sem = semaphore_create(); semaphore_initialize(dead_sem,0); runnable_q_lock = semaphore_create(); semaphore_initialize(runnable_q_lock,1); clean_up_thread = minithread_create(clean_up, NULL); multilevel_queue_enqueue(runnable_q, clean_up_thread->priority,clean_up_thread); runnable_count++; minithread_clock_init(TIME_QUANTA, (interrupt_handler_t)clock_handler); init_alarm(); current_thread = minithread_create(mainproc, mainarg); minithread_switch(&dummy_ptr, &(current_thread->stacktop)); return; }
/* performs any required initialization of the minimsg layer. */ void minimsg_initialize() { int i; msgmutex = semaphore_create(); semaphore_initialize(msgmutex, 1); bound_ports_free = semaphore_create(); semaphore_initialize(bound_ports_free, BOUND_MAX_PORT_NUM - BOUND_MIN_PORT_NUM + 1); // Initialize ports array ports = (miniport_t*) malloc((BOUND_MAX_PORT_NUM + 1) * sizeof(miniport_t)); if (ports == NULL) { // Fail if malloc() fails fprintf(stderr, "ERROR: minimsg_initialize() failed to malloc miniport_t array\n"); return; } // Initialize each unbound port's data elements for (i = UNBOUND_MIN_PORT_NUM; i <= UNBOUND_MAX_PORT_NUM; i++) { miniport_create_unbound(i); ports[i]->u.unbound.incoming_data = queue_new(); ports[i]->u.unbound.datagrams_ready = semaphore_create(); semaphore_initialize(ports[i]->u.unbound.datagrams_ready, 0); } }
/* * Creates a socket * The argument "port" is the port number of the created socket */ minisocket_t minisocket_create_socket(int port) { minisocket_t newMinisocket; newMinisocket = (minisocket_t) malloc(sizeof(struct minisocket)); if (newMinisocket == NULL) return NULL; //Initialize fields newMinisocket->port_number = port; newMinisocket->status = TCP_PORT_LISTENING; newMinisocket->seq_number = 0; newMinisocket->ack_number = 0; newMinisocket->data_buffer = NULL; newMinisocket->data_length = 0; newMinisocket->num_waiting_on_mutex = 0; newMinisocket->timeout = 100; newMinisocket->wait_for_ack_semaphore = semaphore_create(); if (newMinisocket->wait_for_ack_semaphore == NULL) { free(newMinisocket); return NULL; } semaphore_initialize(newMinisocket->wait_for_ack_semaphore, 0); newMinisocket->mutex = semaphore_create(); if (newMinisocket->mutex == NULL) { free(newMinisocket->wait_for_ack_semaphore); free(newMinisocket); return NULL; } semaphore_initialize(newMinisocket->mutex, 1); newMinisocket->packet_ready = semaphore_create(); if (newMinisocket->packet_ready == NULL) { free(newMinisocket->mutex); free(newMinisocket->wait_for_ack_semaphore); free(newMinisocket); return NULL; } semaphore_initialize(newMinisocket->packet_ready, 0); newMinisocket->waiting_packets = queue_new(); if (newMinisocket->waiting_packets == NULL) { free(newMinisocket->packet_ready); free(newMinisocket->mutex); free(newMinisocket->wait_for_ack_semaphore); free(newMinisocket); return NULL; } return newMinisocket; }
int main(int argc, char *argv[]) { sem1 = semaphore_create(); semaphore_initialize(sem1, 0); sem2 = semaphore_create(); semaphore_initialize(sem2, 0); minithread_system_initialize(thread1, NULL); return -1; }
/* This needs to: * -get a block from the in-memory blocks if possible * -otherwise pull block from disk * -increment blocks' num_open counter * -we do NOT need to save the num_open update to disk, as it is reset after a * crash anyway. * * Clients must ALWAYS check the return ID, in case a file was deleted. */ block_t _retrieve_block(int block_id) { block_t block; minifile_t inode; int ret; // todo - handle semaphores for inodes, destroy the in inode delete // First check if it's already in memory if (block_id < 0) return NULL; block = hashmap_get(retrieved_blocks, block_id); if (block != NULL && block->is_deleted == 0) { if ( block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) { block->num_open++; inode = (minifile_t) block; inode->u.data.mutex = semaphore_create(); semaphore_initialize(inode->u.data.mutex, 1); } return block; } // Otherwise, get it from disk block = (block_t) malloc(sizeof(struct block)); ret = _perform_full_disk_read(disk, block_id, (char*) block); // Setup the semaphore if it's an inode type // ONly check deleted for file/dir, as data blocks can have anything if (ret == 0 && !((block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) && block->is_deleted == 1)) { // Ensure num_open is set to just one, as we got it from disk (may have crashed with old data) hashmap_insert(retrieved_blocks, block_id, (void*) block); if ( block->block_type == BLOCK_TYPE_FILE || block->block_type == BLOCK_TYPE_DIRECTORY) { inode = (minifile_t) block; inode->u.data.mutex = semaphore_create(); semaphore_initialize(inode->u.data.mutex, 1); block->num_open = 1; } return block; } else { return NULL; } }
void main(void) { int maxcount = MAXCOUNT; size = head = tail = 0; empty = semaphore_create(); semaphore_initialize(empty, 0); full = semaphore_create(); semaphore_initialize(full, BUFFER_SIZE); minithread_system_initialize(producer, &maxcount); }
void command_move(struct cmdStruct* commandsList, int socket) { char fifoBuffer[__BUFFER_SIZE]; int descr[2]; int stdoutDescr = dup(STDOUT_FILENO); int stdinDescr = dup(STDIN_FILENO); int stderrDescr = dup(STDERR_FILENO); int size = 0; int status; int cStream = pipe(descr); int childPid; int semIdIn = semaphore_create(commandsList->words[1], 1); /* Utworzenie semaforow do zabezpieczenia przed wielokrotną edycją */ int semIdOut = semaphore_create(commandsList->words[2], 1); P(semIdIn, 0); P(semIdOut, 0); childPid = fork(); if (childPid == 0) { dup2(descr[1], 2); /* przekierowanie stderr do FIFO_OUT */ dup2(descr[1], 1); /* przekierowanie stdout do FIFO_OUT */ close(descr[0]); execvp("/bin/mv", commandsList->words); /* jezeli nie uda sie wykonac komendy, zglasza komunikat o bledzie i przywraca wejscia */ dup2(stdinDescr, 0); /* przywrocenie poprzednich wejsc */ dup2(stdoutDescr, 1); dup2(stderrDescr, 2); info_remote_handler(socket, __COMMAND_EXEC_ERROR); } else { dup2(descr[0], 0); /* przekierowanie stdin na FIFO_IN */ close(descr[1]); while (1) { memset(fifoBuffer, 0, __BUFFER_SIZE); size = read(descr[0], fifoBuffer, sizeof(fifoBuffer)); status = write(socket, fifoBuffer, size); if (size == 0) { close(descr[0]); dup2(stdinDescr, 0); /* przywrocenie poprzednich wejsc */ dup2(stdoutDescr, 1); dup2(stderrDescr, 2); break; } } wait(0); V(semIdOut, 0); V(semIdIn, 0); if (semaphore_value_lookup(semIdIn, 0)) semaphore_remove(semIdIn); if (semaphore_value_lookup(semIdOut, 0)) semaphore_remove(semIdOut); } }
int main(int argc, char * argv[]) { int maxcount = MAXCOUNT; size = head = tail = 0; empty = semaphore_create(); semaphore_initialize(empty, 0); full = semaphore_create(); semaphore_initialize(full, BUFFER_SIZE); minithread_system_initialize(producer, &maxcount); return -1; }
int main(void) { pthread_t pthread1, pthread2, pthread3; semaphore_t* sem = g_sem; kern_return_t kr; setbuf(stdout, NULL); kr = semaphore_create(mach_task_self(), &sem[0], SYNC_POLICY_FIFO, 0); OUT_ON_MACH_ERROR("semaphore_create", kr); kr = semaphore_create(mach_task_self(), &sem[1], SYNC_POLICY_FIFO, 0); OUT_ON_MACH_ERROR("semaphore_create", kr); (void)pthread_create(&pthread1, (const pthread_attr_t *)0, start_routine, (void *) 0); printf("created thread1=%lx\n", 0); (void)pthread_create(&pthread2, (const pthread_attr_t *)0, start_routine, (void *) 1); printf("created thread2=%lx\n", 1); (void)pthread_create(&pthread3, (const pthread_attr_t *)0, start_routine, (void *) 2); printf("created thread3=%lx\n", 2); // wait until all three threads are ready SEMAPHORE_WAIT(sem[1], 3); // printf("main: about to signal thread3\n"); // semaphore_signal_thread(sem[0], pthread_mach_thread_np(pthread3)); // wait for thread3 to sem_signal() // semaphore_wait(sem[1]); sleep(1); printf("main: about to signal all threads\n"); semaphore_signal_all(sem[0]); // wait for thread1 and thread2 to sem_signal() SEMAPHORE_WAIT(sem[1], 3); out: if (sem[0]) semaphore_destroy(mach_task_self(), sem[0]); if (sem[1]) semaphore_destroy(mach_task_self(), sem[1]); exit(kr); }
status_t block_device_init_driver (void) { int32_t i ; char alpha_num[8], semaphore_name_buffer[64], * semaphore_prefix = "block_device_" ; char isr_semaphore_name[64] ; watch (status_t) { block_device_controls = kernel_malloc (sizeof (block_device_control_t) * SOCLIB_BLOCK_DEVICES_NDEV, true) ; ensure (block_device_controls != NULL, DNA_OUT_OF_MEM) ; for (i = 0 ; i < SOCLIB_BLOCK_DEVICES_NDEV ; i++) { dna_itoa (i, alpha_num) ; dna_strcpy (semaphore_name_buffer, semaphore_prefix) ; dna_strcat (semaphore_name_buffer, alpha_num) ; dna_strcat (semaphore_name_buffer, "_sem") ; semaphore_create (semaphore_name_buffer, 1, &block_device_controls[i] . semaphore_id) ; block_device_controls[i] . should_enable_irq = (bool) SOCLIB_BLOCK_DEVICES[i] . should_enable_irq ; block_device_controls[i] . port = (block_device_register_map_t) SOCLIB_BLOCK_DEVICES[i] . base_address ; cpu_read (UINT32, & (block_device_controls[i] . port -> BLOCK_DEVICE_SIZE), block_device_controls[i] . block_count) ; cpu_read (UINT32, & (block_device_controls[i] . port -> BLOCK_DEVICE_BLOCK_SIZE), block_device_controls[i] . block_size) ; if (block_device_controls[i] . should_enable_irq) { block_device_controls[i] . irq = SOCLIB_BLOCK_DEVICES[i] . irq ; interrupt_attach (0, SOCLIB_BLOCK_DEVICES[i] . irq, 0x0, block_device_isr, false) ; dna_strcpy (isr_semaphore_name, semaphore_name_buffer) ; dna_strcat (isr_semaphore_name, "_isr") ; semaphore_create (isr_semaphore_name, 0, &block_device_controls[i] . isr_semaphore_id) ; } } return DNA_OK ; } }
/* Create a route request struct - this stores information regarding the current * route discovery attempt. */ route_request_t create_route_request() { route_request_t route_request = (route_request_t) malloc(sizeof(struct route_request)); if (route_request == NULL) return NULL; route_request->threads_waiting = 0; route_request->initiator_sem = semaphore_create(); route_request->waiting_sem = semaphore_create(); semaphore_initialize(route_request->initiator_sem, 0); semaphore_initialize(route_request->waiting_sem, 0); route_request->interrupt_arg = NULL; return route_request; }
s32 sys_semaphore_create(vm::ptr<u32> sem, vm::ptr<sys_semaphore_attribute_t> attr, s32 initial_val, s32 max_val) { sys_semaphore.Warning("sys_semaphore_create(sem=*0x%x, attr=*0x%x, initial_val=%d, max_val=%d)", sem, attr, initial_val, max_val); if (!sem || !attr) { return CELL_EFAULT; } if (max_val <= 0 || initial_val > max_val || initial_val < 0) { sys_semaphore.Error("sys_semaphore_create(): invalid parameters (initial_val=%d, max_val=%d)", initial_val, max_val); return CELL_EINVAL; } const u32 protocol = attr->protocol; switch (protocol) { case SYS_SYNC_FIFO: break; case SYS_SYNC_PRIORITY: break; case SYS_SYNC_PRIORITY_INHERIT: break; default: sys_semaphore.Error("sys_semaphore_create(): unknown protocol (0x%x)", protocol); return CELL_EINVAL; } if (attr->pshared.data() != se32(0x200) || attr->ipc_key.data() || attr->flags.data()) { sys_semaphore.Error("sys_semaphore_create(): unknown attributes (pshared=0x%x, ipc_key=0x%x, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags); return CELL_EINVAL; } *sem = semaphore_create(initial_val, max_val, protocol, attr->name_u64); return CELL_OK; }
void _dispatch_sema4_create_slow(_dispatch_sema4_t *s4, int policy) { semaphore_t tmp = MACH_PORT_NULL; _dispatch_fork_becomes_unsafe(); // lazily allocate the semaphore port // Someday: // 1) Switch to a doubly-linked FIFO in user-space. // 2) User-space timers for the timeout. #if DISPATCH_USE_OS_SEMAPHORE_CACHE if (policy == _DSEMA4_POLICY_FIFO) { tmp = (_dispatch_sema4_t)os_get_cached_semaphore(); if (!os_atomic_cmpxchg(s4, MACH_PORT_NULL, tmp, relaxed)) { os_put_cached_semaphore((os_semaphore_t)tmp); } return; } #endif kern_return_t kr = semaphore_create(mach_task_self(), &tmp, policy, 0); DISPATCH_SEMAPHORE_VERIFY_KR(kr); if (!os_atomic_cmpxchg(s4, MACH_PORT_NULL, tmp, relaxed)) { kr = semaphore_destroy(mach_task_self(), tmp); DISPATCH_SEMAPHORE_VERIFY_KR(kr); } }
void create_data(){ struct semaphore* s=semaphore_create(M); set_bouncer(s); struct BB* bb=BB_create(A); set_ABB(bb); bb=BB_create(A); set_DrinkBB(bb); bb=BB_create(C); set_CBB(bb); // buy new cups int i; for(i=0; i<C; i++){ struct Cup* c=malloc(sizeof(struct Cup)); c->id=next_cupID; next_cupID++; printf(1, "buy new cup\n"); BB_put(cbb, c); } bb=BB_create(C); set_DBB(bb); }
int test4(void) { thread_id tid; sem_id sid; status_t err; int res = 1; /* create new semaphore */ sid = semaphore_create(NULL, 1, 0); if(sid == INVALID_SEMID) return 0; /* create thread */ tid = sys_create_thread(thread_func, &sid, false, 0); if(tid == INVALID_THREADID) return 0; /* wait for created thread count up semaphore */ err = semaphore_down_timeout(sid, 1, 1000); if(err != NO_ERROR) res = 0; /* delete semaphore */ err = semaphore_delete(sid); if(err != NO_ERROR) res = 0; return res; }
Threading::Semaphore::Semaphore() { // other platforms explicitly make a thread-private (unshared) semaphore // here. But it seems Mach doesn't support that. MACH_CHECK(semaphore_create(mach_task_self(), (semaphore_t *)&m_sema, SYNC_POLICY_FIFO, 0)); __atomic_store_n(&m_counter, 0, __ATOMIC_SEQ_CST); }
int miniterm_initialize() { pthread_t read_thread; sigset_t set; sigset_t old_set; sigfillset(&set); sigprocmask(SIG_BLOCK,&set,&old_set); kprintf("Starting read interrupts.\n"); mini_read_handler = read_handler; kb_head = NULL; kb_tail = NULL; new_data = semaphore_create(); semaphore_initialize(new_data, 0); AbortOnCondition(pthread_create(&read_thread, NULL, (void*)read_poll, NULL)!=0, "pthread"); sigdelset(&old_set,SIGRTMAX-2); sigdelset(&old_set,SIGRTMAX-1); pthread_sigmask(SIG_SETMASK,&old_set,NULL); return 0; }
s32 sys_semaphore_create(vm::ptr<be_t<u32>> sem, vm::ptr<sys_semaphore_attribute> attr, s32 initial_count, s32 max_count) { sys_semaphore.Warning("sys_semaphore_create(sem_addr=0x%x, attr_addr=0x%x, initial_count=%d, max_count=%d)", sem.addr(), attr.addr(), initial_count, max_count); if (max_count <= 0 || initial_count > max_count || initial_count < 0) { sys_semaphore.Error("sys_semaphore_create(): invalid parameters (initial_count=%d, max_count=%d)", initial_count, max_count); return CELL_EINVAL; } if (attr->pshared.ToBE() != se32(0x200)) { sys_semaphore.Error("sys_semaphore_create(): invalid pshared value(0x%x)", (u32)attr->pshared); return CELL_EINVAL; } switch (attr->protocol.ToBE()) { case se32(SYS_SYNC_FIFO): break; case se32(SYS_SYNC_PRIORITY): break; case se32(SYS_SYNC_PRIORITY_INHERIT): sys_semaphore.Todo("SYS_SYNC_PRIORITY_INHERIT"); break; case se32(SYS_SYNC_RETRY): sys_semaphore.Error("SYS_SYNC_RETRY"); return CELL_EINVAL; default: sys_semaphore.Error("Unknown protocol attribute(0x%x)", (u32)attr->protocol); return CELL_EINVAL; } *sem = semaphore_create(initial_count, max_count, attr->protocol, attr->name_u64); return CELL_OK; }
/* Creates an unbound port for listening. Multiple requests to create the same * unbound port should return the same miniport reference. It is the responsibility * of the programmer to make sure he does not destroy unbound miniports while they * are still in use by other threads -- this would result in undefined behavior. * Unbound ports must range from 0 to 32767. If the programmer specifies a port number * outside this range, it is considered an error. */ miniport_t miniport_create_unbound(int port_number) { semaphore_P(port_mutex); if (port_number < MIN_UNBOUNDED || port_number > MAX_UNBOUNDED) { semaphore_V(port_mutex); return NULL; } if (port[port_number] != NULL) { semaphore_V(port_mutex); return port[port_number]; } if ((port[port_number] = malloc(sizeof(struct miniport))) != NULL) { port[port_number]->type = UNBOUNDED; port[port_number]->num = port_number; port[port_number]->unbound.data = queue_new(); port[port_number]->unbound.ready = semaphore_create(); if (NULL == port[port_number]->unbound.data || NULL == port[port_number]->unbound.ready) { miniport_destroy(port[port_number]); return NULL; } semaphore_initialize(port[port_number]->unbound.ready, 0); } semaphore_V(port_mutex); return port[port_number]; }
void BGMPlayThrough::Init(BGMAudioDevice inInputDevice, BGMAudioDevice inOutputDevice) { BGMAssert(mInputDeviceIOProcState.is_lock_free(), "BGMPlayThrough::BGMPlayThrough: !mInputDeviceIOProcState.is_lock_free()"); BGMAssert(mOutputDeviceIOProcState.is_lock_free(), "BGMPlayThrough::BGMPlayThrough: !mOutputDeviceIOProcState.is_lock_free()"); BGMAssert(!mActive, "BGMPlayThrough::BGMPlayThrough: Can't init while active."); mInputDevice = inInputDevice; mOutputDevice = inOutputDevice; AllocateBuffer(); try { // Init the semaphore for the output IOProc. if(mOutputDeviceIOProcSemaphore == SEMAPHORE_NULL) { kern_return_t theError = semaphore_create(mach_task_self(), &mOutputDeviceIOProcSemaphore, SYNC_POLICY_FIFO, 0); BGM_Utils::ThrowIfMachError("BGMPlayThrough::BGMPlayThrough", "semaphore_create", theError); ThrowIf(mOutputDeviceIOProcSemaphore == SEMAPHORE_NULL, CAException(kAudioHardwareUnspecifiedError), "BGMPlayThrough::BGMPlayThrough: Could not create semaphore"); } } catch (...) { // Clean up. mBuffer.Deallocate(); throw; } }
int XLinuxTask_preemptive::SemWrapper::Init(uLONG val) { kern_return_t kres=semaphore_create(mach_task_self(), &fSem, SYNC_POLICY_FIFO, 0); xbox_assert(kres==KERN_SUCCESS); return static_cast<int>(kres); }
BOOL InitializeCriticalSectionEx(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags) { /** * See http://msdn.microsoft.com/en-us/library/ff541979(v=vs.85).aspx * - The LockCount field indicates the number of times that any thread has * called the EnterCriticalSection routine for this critical section, * minus one. This field starts at -1 for an unlocked critical section. * Each call of EnterCriticalSection increments this value; each call of * LeaveCriticalSection decrements it. * - The RecursionCount field indicates the number of times that the owning * thread has called EnterCriticalSection for this critical section. */ if (Flags != 0) { WLog_WARN(TAG, "Flags unimplemented"); } lpCriticalSection->DebugInfo = NULL; lpCriticalSection->LockCount = -1; lpCriticalSection->SpinCount = 0; lpCriticalSection->RecursionCount = 0; lpCriticalSection->OwningThread = NULL; lpCriticalSection->LockSemaphore = (winpr_sem_t*) malloc(sizeof(winpr_sem_t)); #if defined(__APPLE__) semaphore_create(mach_task_self(), lpCriticalSection->LockSemaphore, SYNC_POLICY_FIFO, 0); #else sem_init(lpCriticalSection->LockSemaphore, 0, 0); #endif SetCriticalSectionSpinCount(lpCriticalSection, dwSpinCount); return TRUE; }
static void _dispatch_semaphore_create_port(semaphore_t *s4) { kern_return_t kr; semaphore_t tmp; if (*s4) { return; } _dispatch_safe_fork = false; // lazily allocate the semaphore port // Someday: // 1) Switch to a doubly-linked FIFO in user-space. // 2) User-space timers for the timeout. // 3) Use the per-thread semaphore port. while ((kr = semaphore_create(mach_task_self(), &tmp, SYNC_POLICY_FIFO, 0))) { DISPATCH_VERIFY_MIG(kr); sleep(1); } if (!dispatch_atomic_cmpxchg(s4, 0, tmp)) { kr = semaphore_destroy(mach_task_self(), tmp); DISPATCH_SEMAPHORE_VERIFY_KR(kr); } }
/* * net_buffer_init * This function will initialize the buffer file system for out networking * stack. */ void net_buffer_init() { /* Clear the global structure. */ memset(&net_buffers_fs, 0, sizeof(NET_BUFFER_FS)); /* Initialize the buffer data. */ net_buffers_fs.fs.name = "\\net\\buffers"; net_buffers_fs.fs.get_lock = &net_buffer_lock; net_buffers_fs.fs.release_lock = &net_buffer_unlock; /* Read and write functions. */ net_buffers_fs.fs.write = &net_buffer_write; net_buffers_fs.fs.read = &net_buffer_read; /* Initial file system configurations. */ net_buffers_fs.fs.flags = (FS_SPACE_AVAILABLE); net_buffers_fs.fs.timeout = MAX_WAIT; /* Initialize file system condition. */ fs_condition_init(&net_buffers_fs.fs); #ifdef CONFIG_SEMAPHORE /* Create a semaphore to protect net buffer file descriptor. */ semaphore_create(&net_buffers_fs.lock, 1, 1, SEMAPHORE_PRIORITY); #endif /* Register net buffer file system. */ fs_register((FS *)&net_buffers_fs); /* Set the global networking stack buffer descriptor. */ net_buff_fd = (FD)&net_buffers_fs; } /* net_buffer_init */
/* * sleep with timeout in milliseconds */ void minithread_sleep_with_timeout(int delay) { // Create the sleep semaphore semaphore_t sleep_sem = semaphore_create(); interrupt_level_t prev_level; // ISO C90... // Initialize it to a value of 0 so it can act as a way to signal threads semaphore_initialize(sleep_sem, 0); // Disable interrupts prev_level = set_interrupt_level(DISABLED); // Register the alarm register_alarm(delay, &minithread_sleep_alarm_wakeup, sleep_sem); // If, at this point, interrupts were enabled, we could context switch away // the alarm could be triggered afterwards before the semaphore_P below was // called (depending on this threads priority level, etc) and then when this // thread finally calls semaphore_P(), it will hang forever. // Therefore we have interrupts disabled. // Wait on the sleep semaphore semaphore_P(sleep_sem); // Now that we've awoken, free the sleep semaphore semaphore_destroy(sleep_sem); // Restore the previous interrupt level set_interrupt_level(prev_level); // is this necessary? }
void HighPrecisionClock::wait(uint64_t expirationTime) { semaphore_t *sem = threadSpecificSemaphore.get(); if (!sem) { semaphore_t newSem; if (logMachError("semaphore_create", semaphore_create(mach_task_self(), &newSem, SYNC_POLICY_FIFO, 0)) || (threadSpecificSemaphore.reset(new semaphore_t(newSem)), false) || // NOTE: This final test looks unnecessary, but omitting it leads to server crashes when using // Boost 1.60.0 !(sem = threadSpecificSemaphore.get())) { // If we can't create the semaphore, do a low-precision wait with mach_wait_until, and hope // that semaphore_create will work next time if (0 == expirationTime) { expirationTime = mach_absolute_time() + period; } logMachError("mach_wait_until", mach_wait_until(expirationTime)); return; } } { lock_guard lock(waitsMutex); waits.push(WaitInfo(expirationTime, *sem)); } logMachError("semaphore_wait", semaphore_wait(*sem)); }