1 /* os_mutex.h -*-C++-*-
3 *************************************************************************
6 * Copyright (C) 2009-2013, Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 **************************************************************************/
42 * @brief Portable interface to operating-system mutexes.
44 * Do not confuse os_mutex with Cilk runtime-specific spinlock mutexes.
47 #ifndef INCLUDED_OS_MUTEX_DOT_H
48 #define INCLUDED_OS_MUTEX_DOT_H
50 #include <cilk/common.h>
51 #include "rts-common.h"
53 __CILKRTS_BEGIN_EXTERN_C
56 typedef struct os_mutex os_mutex
;
59 * Allocate and initialize an os_mutex
61 * @return A pointer to the initialized os_mutex
63 COMMON_SYSDEP os_mutex
* __cilkrts_os_mutex_create(void);
66 * Acquire the os_mutex for exclusive use
68 * @param m The os_mutex that is to be acquired.
70 COMMON_SYSDEP
void __cilkrts_os_mutex_lock(os_mutex
*m
);
73 * Try to acquire the os_mutex.
75 * @param m The os_mutex to try to acquire
76 * @return 0 if the lock acquire failed
77 * @return nonzero if the lock was acquired
79 COMMON_SYSDEP
int __cilkrts_os_mutex_trylock(os_mutex
*m
);
82 * Release the os_mutex
84 * @param m The os_mutex that is to be released.
86 COMMON_SYSDEP
void __cilkrts_os_mutex_unlock(os_mutex
*m
);
89 * Release any resources and deallocate the os_mutex.
91 * @param m The os_mutex that is to be deallocated.
93 COMMON_SYSDEP
void __cilkrts_os_mutex_destroy(os_mutex
*m
);
96 * Acquire the global os_mutex for exclusive use. The global os_mutex
97 * will be initialized the first time this function is called in a
100 COMMON_SYSDEP
void global_os_mutex_lock();
103 * Release the global os_mutex. global_os_mutex_lock() must have been
106 COMMON_SYSDEP
void global_os_mutex_unlock();
112 * @brief Create the global OS mutex - Windows only.
114 * On Windows we use DllMain() to create the global OS mutex when cilkrts20.dll
115 * is loaded. As opposed to Linux/MacOS where we use pthread_once to implement
116 * a singleton since there are no guarantees about constructor or destructor
117 * ordering between shared objects.
119 NON_COMMON
void global_os_mutex_create();
122 * @brief Destroy the global OS mutex - Windows only
124 * On Windows we use DllMain() to destroy the global OS mutex when
125 * cilkrts20.dll is unloaded. As opposed to Linux/MacOS where we cannot
126 * know when it's safe to destroy the global OS mutex since there are no
127 * guarantees about constructor or destructor ordering.
129 NON_COMMON
void global_os_mutex_destroy();
133 __CILKRTS_END_EXTERN_C
135 #endif // ! defined(INCLUDED_OS_MUTEX_DOT_H)