alokp@chromium.org | 277ec18 | 2010-04-23 16:07:34 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // This file contains the posix specific functions |
| 9 | // |
| 10 | #include "compiler/osinclude.h" |
| 11 | |
| 12 | #if !defined(ANGLE_OS_POSIX) |
| 13 | #error Trying to build a posix specific file in a non-posix build. |
| 14 | #endif |
| 15 | |
| 16 | // |
| 17 | // Thread cleanup |
| 18 | // |
| 19 | |
| 20 | // |
| 21 | // Wrapper for Linux call to DetachThread. |
| 22 | // This is required as pthread_cleanup_push() expects the cleanup routine |
| 23 | // to return void. |
| 24 | // |
| 25 | void DetachThreadLinux(void *) |
| 26 | { |
| 27 | DetachThread(); |
| 28 | } |
| 29 | |
| 30 | // |
| 31 | // Registers cleanup handler, sets cancel type and state, and excecutes |
| 32 | // the thread specific cleanup handler. When OpenGL applications are run with |
| 33 | // the driver code, Linux OS does the thread cleanup. |
| 34 | // |
| 35 | void OS_CleanupThreadData(void) |
| 36 | { |
| 37 | int old_cancel_state, old_cancel_type; |
| 38 | void *cleanupArg = NULL; |
| 39 | |
| 40 | // |
| 41 | // Set thread cancel state and push cleanup handler. |
| 42 | // |
| 43 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancel_state); |
| 44 | pthread_cleanup_push(DetachThreadLinux, (void *) cleanupArg); |
| 45 | |
| 46 | // |
| 47 | // Put the thread in deferred cancellation mode. |
| 48 | // |
| 49 | pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &old_cancel_type); |
| 50 | |
| 51 | // |
| 52 | // Pop cleanup handler and execute it prior to unregistering |
| 53 | // the cleanup handler. |
| 54 | // |
| 55 | pthread_cleanup_pop(1); |
| 56 | |
| 57 | // |
| 58 | // Restore the thread's previous cancellation mode. |
| 59 | // |
| 60 | pthread_setcanceltype(old_cancel_state, NULL); |
| 61 | } |
| 62 | |
| 63 | // |
| 64 | // Thread Local Storage Operations |
| 65 | // |
| 66 | OS_TLSIndex OS_AllocTLSIndex() |
| 67 | { |
| 68 | pthread_key_t pPoolIndex; |
| 69 | |
| 70 | // |
| 71 | // Create global pool key. |
| 72 | // |
| 73 | if ((pthread_key_create(&pPoolIndex, NULL)) != 0) { |
| 74 | assert(0 && "OS_AllocTLSIndex(): Unable to allocate Thread Local Storage"); |
| 75 | return false; |
| 76 | } |
| 77 | else { |
| 78 | return pPoolIndex; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | bool OS_SetTLSValue(OS_TLSIndex nIndex, void *lpvValue) |
| 84 | { |
| 85 | if (nIndex == OS_INVALID_TLS_INDEX) { |
| 86 | assert(0 && "OS_SetTLSValue(): Invalid TLS Index"); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (pthread_setspecific(nIndex, lpvValue) == 0) |
| 91 | return true; |
| 92 | else |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | bool OS_FreeTLSIndex(OS_TLSIndex nIndex) |
| 98 | { |
| 99 | if (nIndex == OS_INVALID_TLS_INDEX) { |
| 100 | assert(0 && "OS_SetTLSValue(): Invalid TLS Index"); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | // |
| 105 | // Delete the global pool key. |
| 106 | // |
| 107 | if (pthread_key_delete(nIndex) == 0) |
| 108 | return true; |
| 109 | else |
| 110 | return false; |
| 111 | } |