Skip to content

Commit

Permalink
Removed padding from OE_Mutex and OE_Cond.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikbras committed Jan 23, 2018
1 parent fdb7462 commit 30d89a6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 36 deletions.
8 changes: 8 additions & 0 deletions 3rdparty/libunwind/libunwind/include/libunwind_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ static inline void mark_as_used(void *v UNUSED) {
# define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
#endif

/* Disable use of adaptive mutexes, which are defined by GCC headers but not
* supported by MUSL pthreads. Note that libunwind is compiled with GCC headers
* but linked with MUSL libc.
*/
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
#undef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
#endif

/* Prefer adaptive mutexes if available */
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
#define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
Expand Down
72 changes: 62 additions & 10 deletions enclave/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,31 @@ int OE_ThreadEqual(OE_Thread thread1, OE_Thread thread2)
**==============================================================================
*/

int OE_MutexInit(OE_Mutex* m)
/* Internal mutex implementation */
typedef struct _OE_MutexImpl
{
/* Lock used to synchronize access to OE_ThreadData queue */
OE_Spinlock lock;

/* Number of references to support recursiving locking */
unsigned int refs;

/* Queue of waiting threads (front holds the mutex) */
struct
{
OE_ThreadData* front;
OE_ThreadData* back;
}
queue;
}
OE_MutexImpl;

OE_STATIC_ASSERT(sizeof(OE_MutexImpl) <= sizeof(OE_Mutex));

int OE_MutexInit(OE_Mutex* mutex)
{
OE_MutexImpl* m = (OE_MutexImpl*)mutex;

if (m)
{
OE_Memset(m, 0, sizeof(OE_Mutex));
Expand All @@ -153,8 +176,9 @@ int OE_MutexInit(OE_Mutex* m)
return 0;
}

int OE_MutexLock(OE_Mutex* m)
int OE_MutexLock(OE_Mutex* mutex)
{
OE_MutexImpl* m = (OE_MutexImpl*)mutex;
OE_ThreadData* self = OE_GetThreadData();

if (!m)
Expand Down Expand Up @@ -188,8 +212,9 @@ int OE_MutexLock(OE_Mutex* m)
/* Unreachable! */
}

int OE_MutexTryLock(OE_Mutex* m)
int OE_MutexTryLock(OE_Mutex* mutex)
{
OE_MutexImpl* m = (OE_MutexImpl*)mutex;
OE_ThreadData* self = OE_GetThreadData();

if (!m)
Expand Down Expand Up @@ -220,8 +245,9 @@ int OE_MutexTryLock(OE_Mutex* m)
return -1;
}

static int _MutexUnlock(OE_Mutex* m, OE_ThreadData** waiter)
static int _MutexUnlock(OE_Mutex* mutex, OE_ThreadData** waiter)
{
OE_MutexImpl* m = (OE_MutexImpl*)mutex;
OE_ThreadData* self = OE_GetThreadData();
int ret = -1;

Expand Down Expand Up @@ -268,8 +294,9 @@ int OE_MutexUnlock(OE_Mutex* m)
}

int OE_MutexDestroy(
OE_Mutex* m)
OE_Mutex* mutex)
{
OE_MutexImpl* m = (OE_MutexImpl*)mutex;
int ret = -1;

if (!m)
Expand Down Expand Up @@ -297,8 +324,28 @@ int OE_MutexDestroy(
**==============================================================================
*/

int OE_CondInit(OE_Cond* cond)
/* Internal condition variable implementation */
typedef struct _OE_CondImpl
{
/* Spinlock for synchronizing access to thread queue and mutex parameter */
OE_Spinlock lock;

/* Queue of threads waiting on this condition variable */
struct
{
OE_ThreadData* front;
OE_ThreadData* back;
}
queue;
}
OE_CondImpl;

OE_STATIC_ASSERT(sizeof(OE_CondImpl) <= sizeof(OE_Cond));

int OE_CondInit(OE_Cond* condition)
{
OE_CondImpl* cond = (OE_CondImpl*)condition;

if (cond)
{
OE_Memset(cond, 0, sizeof(OE_Cond));
Expand All @@ -309,8 +356,10 @@ int OE_CondInit(OE_Cond* cond)
}

int OE_CondDestroy(
OE_Cond* cond)
OE_Cond* condition)
{
OE_CondImpl* cond = (OE_CondImpl*)condition;

if (!cond)
return -1;

Expand All @@ -329,9 +378,10 @@ int OE_CondDestroy(
}

int OE_CondWait(
OE_Cond* cond,
OE_Cond* condition,
OE_Mutex* mutex)
{
OE_CondImpl* cond = (OE_CondImpl*)condition;
OE_ThreadData* self = OE_GetThreadData();

OE_SpinLock(&cond->lock);
Expand Down Expand Up @@ -376,8 +426,9 @@ int OE_CondWait(
}

int OE_CondSignal(
OE_Cond* cond)
OE_Cond* condition)
{
OE_CondImpl* cond = (OE_CondImpl*)condition;
OE_ThreadData* waiter;

OE_SpinLock(&cond->lock);
Expand All @@ -392,8 +443,9 @@ int OE_CondSignal(
}

int OE_CondBroadcast(
OE_Cond* cond)
OE_Cond* condition)
{
OE_CondImpl* cond = (OE_CondImpl*)condition;
Queue waiters = { NULL, NULL };

OE_SpinLock(&cond->lock);
Expand Down
33 changes: 7 additions & 26 deletions include/openenclave/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef uint64_t OE_Thread;

typedef struct _OE_ThreadAttr
{
/* Internal private implementation */
uint64_t __impl[7];
}
OE_ThreadAttr;
Expand Down Expand Up @@ -143,27 +144,16 @@ int OE_SpinUnlock(
*/
int OE_SpinDestroy(OE_Spinlock* spinlock);

#define OE_MUTEX_INITIALIZER \
{OE_SPINLOCK_INITIALIZER,0,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{NULL,NULL}}
#define OE_MUTEX_INITIALIZER {{0}}

/* Definition of a mutex */
typedef struct _OE_Mutex
{
OE_Spinlock lock;
unsigned int refs;
unsigned char __padding[16]; /* align with system pthread_t */
struct
{
void* front;
void* back;
}
queue;
/* Internal private implementation */
uint64_t __impl[8];
}
OE_Mutex;

/* This must be the same size as pthread_mutex_t in GLIBC */
OE_STATIC_ASSERT((sizeof(OE_Mutex) == 40));

/**
* Initialize a mutex.
*
Expand Down Expand Up @@ -235,25 +225,16 @@ int OE_MutexUnlock(OE_Mutex* mutex);
*/
int OE_MutexDestroy(OE_Mutex* mutex);

#define OE_COND_INITIALIZER {OE_SPINLOCK_INITIALIZER}
#define OE_COND_INITIALIZER {{0}}

/* Condition variable representation */
typedef struct _OE_Cond
{
OE_Spinlock lock;
struct
{
void* front;
void* back;
}
queue;
uint64_t padding[3];
/* Internal private implementation */
uint64_t __impl[8];
}
OE_Cond;

/* This must the same size as pthread_cond_t in GLIBC */
OE_STATIC_ASSERT((sizeof(OE_Cond) == 48));

/**
* Initializes a condition variable.
*
Expand Down

0 comments on commit 30d89a6

Please sign in to comment.