1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "kernel.h"
6 
7 #include <new>
8 #include <stdio.h>
9 
10 #include <fbl/mutex.h>
11 #include <zircon/assert.h>
12 
13 // Gets a semaphore token.
14 //
15 // Inputs: sem = pointer to semaphore control block.
16 //         wait_opt = 0 (no wait), -1 (wait forever), or positive timeout count.
17 //
18 // Returns: 0 if successful, else -1 with errno set to error code.
semPend(SEM sem,int wait_opt)19 int semPend(SEM sem, int wait_opt) __TA_ACQUIRE(sem) {
20     ZX_DEBUG_ASSERT(wait_opt == WAIT_FOREVER);
21     reinterpret_cast<fbl::Mutex*>(sem)->Acquire();
22     return 0;
23 }
24 
25 // Returns a semaphore token, ensures not already released.
semPostBin(SEM sem)26 void semPostBin(SEM sem) __TA_RELEASE(sem) {
27     reinterpret_cast<fbl::Mutex*>(sem)->Release();
28 }
29 
30 // Creates and initialize semaphore.
31 //
32 // Inputs: name = ASCII string of semaphore name.
33 //         count = initial semaphore count.
34 //         mode = task queuing mode: OS_FIFO (unused).
35 //
36 // Returns: ID of new semaphore, or NULL if error
semCreate(const char name[8],int init_count,int mode)37 SEM semCreate(const char name[8], int init_count, int mode) {
38     ZX_DEBUG_ASSERT(init_count == 1);
39     fbl::Mutex* mutex = new fbl::Mutex();
40     SEM sem = reinterpret_cast<SEM>(mutex);
41     return sem;
42 }
43 
44 // Deletes specified semaphore, freeing its control block and any pending tasks.
semDelete(SEM * semp)45 void semDelete(SEM* semp) {
46     fbl::Mutex* mutex = reinterpret_cast<fbl::Mutex*>(*semp);
47     delete mutex;
48     *semp = nullptr;
49 }
50