2 POSIX Pthreads to emulate APs and implement threads
4 Copyright (c) 2011, Apple Inc. All rights reserved.
5 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 return (UINTN
)pthread_mutex_lock ((pthread_mutex_t
*)Mutex
);
39 return (UINTN
)pthread_mutex_unlock ((pthread_mutex_t
*)Mutex
);
49 return (UINTN
)pthread_mutex_trylock ((pthread_mutex_t
*)Mutex
);
58 pthread_mutex_t
*Mutex
;
61 Mutex
= malloc (sizeof (pthread_mutex_t
));
62 err
= pthread_mutex_init (Mutex
, NULL
);
77 return pthread_mutex_destroy ((pthread_mutex_t
*)Mutex
);
83 // Can't store this data on PthreadCreate stack so we need a global
85 pthread_mutex_t Mutex
;
86 THREAD_THUNK_THREAD_ENTRY Start
;
89 THREAD_MANGLE mThreadMangle
= {
90 PTHREAD_MUTEX_INITIALIZER
,
99 THREAD_THUNK_THREAD_ENTRY Start
;
102 // Save global on the stack before we unlock
103 Start
= mThreadMangle
.Start
;
104 pthread_mutex_unlock (&mThreadMangle
.Mutex
);
106 // Mask all signals to the APs
107 sigfillset (&SigMask
);
108 pthread_sigmask (SIG_BLOCK
, &SigMask
, NULL
);
111 // We have to start the thread in SEC as we need to follow
112 // OS X calling conventions. We can then call back into
113 // to the callers Start.
115 // This is a great example of how all problems in computer
116 // science can be solved by adding another level of indirection
118 return (VOID
*)ReverseGasketUint64 ((CALL_BACK
)Start
, (UINTN
)Context
);
125 IN THREAD_THUNK_THREAD_ENTRY Start
,
130 BOOLEAN EnabledOnEntry
;
133 // Threads inherit interrupt state so disable interrupts before we start thread
135 if (SecInterruptEanbled ()) {
136 SecDisableInterrupt ();
137 EnabledOnEntry
= TRUE
;
139 EnabledOnEntry
= FALSE
;
142 // Aquire lock for global, SecFakePthreadStart runs in a different thread.
143 pthread_mutex_lock (&mThreadMangle
.Mutex
);
144 mThreadMangle
.Start
= Start
;
146 err
= pthread_create (Thread
, Attribute
, SecFakePthreadStart
, Context
);
148 // Thread failed to launch so release the lock;
149 pthread_mutex_unlock (&mThreadMangle
.Mutex
);
152 if (EnabledOnEntry
) {
153 // Restore interrupt state
154 SecEnableInterrupt ();
166 pthread_exit (ValuePtr
);
176 // POSIX currently allows pthread_t to be a structure or arithmetic type.
177 // Check out sys/types.h to make sure this will work if you are porting.
178 // On OS X (Darwin) pthread_t is a pointer to a structure so this code works.
179 return (UINTN
)pthread_self ();
183 EMU_THREAD_THUNK_PROTOCOL gPthreadThunk
= {
184 GasketPthreadMutexLock
,
185 GasketPthreadMutexUnLock
,
186 GasketPthreadMutexTryLock
,
187 GasketPthreadMutexInit
,
188 GasketPthreadMutexDestroy
,
197 IN EMU_IO_THUNK_PROTOCOL
*This
200 if (This
->Instance
!= 0) {
201 // Only single instance is supported
202 return EFI_NOT_FOUND
;
205 if (This
->ConfigString
[0] == L
'0') {
206 // If AP count is zero no need for threads
207 return EFI_NOT_FOUND
;
210 This
->Interface
= &gPthreadThunk
;
218 IN EMU_IO_THUNK_PROTOCOL
*This
225 EMU_IO_THUNK_PROTOCOL gPthreadThunkIo
= {
226 &gEmuThreadThunkProtocolGuid
,