Skip to content

Commit ca3795b

Browse files
authored
Change some type definitions in pthread. (#738)
Make sure the pthread structures be initialized by PTHREAD_*_INITIALIZER. Also fix one potential race condition issue. Signed-off-by: Zhang Lili <lili.z.zhang@intel.com>
1 parent ebcab67 commit ca3795b

File tree

4 files changed

+30
-216
lines changed

4 files changed

+30
-216
lines changed

common/inc/tlibc/pthread.h

+13-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define _PTHREAD_H_
2626

2727
#include <sgx_defs.h>
28+
#include <sgx_thread.h>
2829

2930
/*
3031
* Flags for once initialization.
@@ -40,9 +41,9 @@
4041
/*
4142
* Static initialization values.
4243
*/
43-
#define PTHREAD_MUTEX_INITIALIZER NULL
44-
#define PTHREAD_COND_INITIALIZER NULL
45-
#define PTHREAD_RWLOCK_INITIALIZER NULL
44+
#define PTHREAD_MUTEX_INITIALIZER SGX_THREAD_MUTEX_INITIALIZER
45+
#define PTHREAD_COND_INITIALIZER SGX_THREAD_COND_INITIALIZER
46+
#define PTHREAD_RWLOCK_INITIALIZER SGX_THREAD_LOCK_INITIALIZER
4647

4748
/*
4849
* Primitive system data type definitions required by P1003.1c.
@@ -51,18 +52,15 @@
5152
* or assignment operators for the types pthread_attr_t, pthread_cond_t,
5253
* pthread_condattr_t, pthread_mutex_t, pthread_mutexattr_t.
5354
*/
54-
typedef struct _pthread *pthread_t;
55-
typedef struct _pthread_attr *pthread_attr_t;
56-
typedef struct _sgx_thread_mutex_t *pthread_mutex_t;
57-
typedef struct _sgx_thread_mutex_attr_t *pthread_mutexattr_t;
58-
typedef struct _sgx_thread_cond_t *pthread_cond_t;
59-
typedef struct _sgx_thread_cond_attr_t *pthread_condattr_t;
60-
typedef int pthread_key_t;
61-
typedef struct _sgx_thread_rwlock_t *pthread_rwlock_t;
62-
typedef struct _sgx_thread_rwlockattr_t *pthread_rwlockattr_t;
63-
//typedef struct pthread_barrier *pthread_barrier_t;
64-
//typedef struct pthread_barrierattr *pthread_barrierattr_t;
65-
//typedef struct pthread_spinlock *pthread_spinlock_t;
55+
typedef struct _pthread *pthread_t;
56+
typedef struct _pthread_attr *pthread_attr_t;
57+
typedef struct _sgx_thread_mutex_t pthread_mutex_t;
58+
typedef struct _sgx_thread_mutex_attr_t pthread_mutexattr_t;
59+
typedef struct _sgx_thread_cond_t pthread_cond_t;
60+
typedef struct _sgx_thread_cond_attr_t pthread_condattr_t;
61+
typedef int pthread_key_t;
62+
typedef struct _sgx_thread_rwlock_t pthread_rwlock_t;
63+
typedef struct _sgx_thread_rwlockattr_t pthread_rwlockattr_t;
6664

6765
/*
6866
* Once definitions.

sdk/pthread/pthread_cond.cpp

+5-35
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,26 @@
2929
int pthread_cond_init(pthread_cond_t *condp, const pthread_condattr_t *attr)
3030
{
3131
UNUSED(attr);
32-
pthread_cond_t cond;
33-
cond = (pthread_cond_t)calloc(1, sizeof(*cond));
34-
if (cond == NULL)
35-
return (ENOMEM);
36-
*condp = cond;
37-
return sgx_thread_cond_init(cond, NULL);
32+
return sgx_thread_cond_init(condp, NULL);
3833
}
3934

4035
int pthread_cond_destroy(pthread_cond_t *condp)
4136
{
42-
int error;
43-
pthread_cond_t cond;
44-
cond = *condp;
45-
if (cond != NULL) {
46-
error = sgx_thread_cond_destroy(cond);
47-
if(0 != error)
48-
return error;
49-
free(cond);
50-
}
51-
*condp = NULL;
52-
return 0;
37+
return sgx_thread_cond_destroy(condp);
5338
}
5439

5540
int pthread_cond_wait(pthread_cond_t *condp, pthread_mutex_t *mutexp)
5641
{
57-
pthread_cond_t cond;
58-
int error;
59-
if (*condp == NULL) {
60-
if ((error = pthread_cond_init(condp, NULL)))
61-
return (error);
62-
}
63-
cond = *condp;
64-
return sgx_thread_cond_wait(cond, *mutexp);
42+
return sgx_thread_cond_wait(condp, mutexp);
6543
}
6644

6745
int pthread_cond_signal(pthread_cond_t *condp)
6846
{
69-
pthread_cond_t cond;
70-
if (*condp == NULL)
71-
return (0);
72-
cond = *condp;
73-
return sgx_thread_cond_signal(cond);
47+
return sgx_thread_cond_signal(condp);
7448
}
7549

7650
int pthread_cond_broadcast(pthread_cond_t *condp)
7751
{
78-
pthread_cond_t cond;
79-
if (*condp == NULL)
80-
return (0);
81-
cond = *condp;
82-
return sgx_thread_cond_broadcast(cond);
52+
return sgx_thread_cond_broadcast(condp);
8353
}
8454

sdk/pthread/pthread_mutex.cpp

+5-67
Original file line numberDiff line numberDiff line change
@@ -28,89 +28,27 @@ static volatile uint32_t static_init_lock = SGX_SPINLOCK_INITIALIZER;
2828

2929
int pthread_mutex_init(pthread_mutex_t *mutexp, const pthread_mutexattr_t *attr)
3030
{
31-
pthread_mutex_t mutex;
3231
UNUSED(attr);
33-
mutex = (pthread_mutex_t)calloc(1, sizeof(*mutex));
34-
if (mutex == NULL)
35-
return (ENOMEM);
36-
sgx_thread_mutex_init(mutex, NULL);
37-
*mutexp = mutex;
38-
return 0;
32+
return sgx_thread_mutex_init(mutexp, NULL);
3933
}
4034

4135
int pthread_mutex_destroy(pthread_mutex_t *mutexp)
4236
{
43-
if (mutexp == NULL)
44-
return (EINVAL);
45-
46-
sgx_thread_mutex_t *mutex = *mutexp;
47-
if(mutex) {
48-
int ret = sgx_thread_mutex_destroy(mutex);
49-
if(ret != 0)
50-
return ret;
51-
52-
free((void *)(mutex));
53-
*mutexp = NULL;
54-
}
55-
return 0;
37+
return sgx_thread_mutex_destroy(mutexp);
5638
}
5739

5840
int pthread_mutex_lock(pthread_mutex_t *mutexp)
5941
{
60-
if (mutexp == NULL)
61-
return (EINVAL);
62-
int error=0;
63-
64-
/*
65-
* If the mutex is statically initialized, perform the dynamic
66-
* initialization. Note: _thread_mutex_lock() in libc requires
67-
* pthread_mutex_lock() to perform the mutex init when *mutexp
68-
* is NULL.
69-
*/
70-
if (*mutexp == NULL) {
71-
sgx_spin_lock(&static_init_lock);
72-
if (*mutexp == NULL)
73-
error = pthread_mutex_init(mutexp, NULL);
74-
sgx_spin_unlock(&static_init_lock);
75-
if (error != 0)
76-
return (EINVAL);
77-
}
78-
79-
return sgx_thread_mutex_lock(*mutexp);
42+
return sgx_thread_mutex_lock(mutexp);
8043
}
8144

8245
int pthread_mutex_trylock(pthread_mutex_t *mutexp)
8346
{
84-
if (mutexp == NULL)
85-
return (EINVAL);
86-
int error=0;
87-
88-
/*
89-
* If the mutex is statically initialized, perform the dynamic
90-
* initialization. Note: _thread_mutex_lock() in libc requires
91-
* pthread_mutex_lock() to perform the mutex init when *mutexp
92-
* is NULL.
93-
*/
94-
if (*mutexp == NULL) {
95-
sgx_spin_lock(&static_init_lock);
96-
if (*mutexp == NULL)
97-
error = pthread_mutex_init(mutexp, NULL);
98-
sgx_spin_unlock(&static_init_lock);
99-
if (error != 0)
100-
return (EINVAL);
101-
}
102-
103-
return sgx_thread_mutex_trylock(*mutexp);
47+
return sgx_thread_mutex_trylock(mutexp);
10448
}
10549

10650
int pthread_mutex_unlock(pthread_mutex_t *mutexp)
10751
{
108-
if (mutexp == NULL)
109-
return (EINVAL);
110-
111-
if (*mutexp == NULL)
112-
abort();
113-
114-
return sgx_thread_mutex_unlock(*mutexp);
52+
return sgx_thread_mutex_unlock(mutexp);
11553
}
11654

sdk/pthread/pthread_rwlock.cpp

+7-99
Original file line numberDiff line numberDiff line change
@@ -28,131 +28,39 @@ static volatile uint32_t static_init_lock = SGX_SPINLOCK_INITIALIZER;
2828

2929
int pthread_rwlock_init(pthread_rwlock_t *rwlockp, const pthread_rwlockattr_t *attr)
3030
{
31-
pthread_rwlock_t rwlock;
3231
UNUSED(attr);
33-
rwlock = (pthread_rwlock_t)calloc(1, sizeof(*rwlock));
34-
if (rwlock == NULL)
35-
return (ENOMEM);
36-
sgx_thread_rwlock_init(rwlock, NULL);
37-
*rwlockp = rwlock;
38-
return 0;
32+
return sgx_thread_rwlock_init(rwlockp, NULL);
3933
}
4034

4135
int pthread_rwlock_destroy(pthread_rwlock_t *rwlockp)
4236
{
43-
if (rwlockp == NULL)
44-
return (EINVAL);
45-
46-
sgx_thread_rwlock_t *rwlock = *rwlockp;
47-
if(rwlock) {
48-
int ret = sgx_thread_rwlock_destroy(rwlock);
49-
if(ret != 0)
50-
return ret;
51-
52-
free((void *)(rwlock));
53-
*rwlockp = NULL;
54-
}
55-
return 0;
37+
return sgx_thread_rwlock_destroy(rwlockp);
5638
}
5739

5840
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlockp)
5941
{
60-
if (rwlockp == NULL)
61-
return (EINVAL);
62-
int error = 0;
63-
64-
/*
65-
* If the rwlock is statically initialized, perform the dynamic
66-
* initialization.
67-
*/
68-
if (*rwlockp == NULL) {
69-
sgx_spin_lock(&static_init_lock);
70-
if (*rwlockp == NULL)
71-
error = pthread_rwlock_init(rwlockp, NULL);
72-
sgx_spin_unlock(&static_init_lock);
73-
if (error != 0)
74-
return (EINVAL);
75-
}
76-
77-
return sgx_thread_rwlock_rdlock(*rwlockp);
42+
return sgx_thread_rwlock_rdlock(rwlockp);
7843
}
7944

8045
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlockp)
8146
{
82-
if (rwlockp == NULL)
83-
return (EINVAL);
84-
int error=0;
85-
86-
/*
87-
* If the rwlock is statically initialized, perform the dynamic
88-
* initialization.
89-
*/
90-
if (*rwlockp == NULL) {
91-
sgx_spin_lock(&static_init_lock);
92-
if (*rwlockp == NULL)
93-
error = pthread_rwlock_init(rwlockp, NULL);
94-
sgx_spin_unlock(&static_init_lock);
95-
if (error != 0)
96-
return (EINVAL);
97-
}
98-
99-
return sgx_thread_rwlock_tryrdlock(*rwlockp);
47+
return sgx_thread_rwlock_tryrdlock(rwlockp);
10048
}
10149

10250

10351
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlockp)
10452
{
105-
if (rwlockp == NULL)
106-
return (EINVAL);
107-
int error = 0;
108-
109-
/*
110-
* If the rwlock is statically initialized, perform the dynamic
111-
* initialization.
112-
*/
113-
if (*rwlockp == NULL) {
114-
sgx_spin_lock(&static_init_lock);
115-
if (*rwlockp == NULL)
116-
error = pthread_rwlock_init(rwlockp, NULL);
117-
sgx_spin_unlock(&static_init_lock);
118-
if (error != 0)
119-
return (EINVAL);
120-
}
121-
122-
return sgx_thread_rwlock_wrlock(*rwlockp);
53+
return sgx_thread_rwlock_wrlock(rwlockp);
12354
}
12455

12556

12657
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlockp)
12758
{
128-
if (rwlockp == NULL)
129-
return (EINVAL);
130-
int error = 0;
131-
132-
/*
133-
* If the rwlock is statically initialized, perform the dynamic
134-
* initialization.
135-
*/
136-
if (*rwlockp == NULL) {
137-
sgx_spin_lock(&static_init_lock);
138-
if (*rwlockp == NULL)
139-
error = pthread_rwlock_init(rwlockp, NULL);
140-
sgx_spin_unlock(&static_init_lock);
141-
if (error != 0)
142-
return (EINVAL);
143-
}
144-
145-
return sgx_thread_rwlock_trywrlock(*rwlockp);
59+
return sgx_thread_rwlock_trywrlock(rwlockp);
14660
}
14761

14862
int pthread_rwlock_unlock(pthread_rwlock_t *rwlockp)
14963
{
150-
if (rwlockp == NULL)
151-
return (EINVAL);
152-
153-
if (*rwlockp == NULL)
154-
abort();
155-
156-
return sgx_thread_rwlock_unlock(*rwlockp);
64+
return sgx_thread_rwlock_unlock(rwlockp);
15765
}
15866

0 commit comments

Comments
 (0)