aboutsummaryrefslogtreecommitdiff
path: root/libcilkrts/runtime/spin_mutex.h
blob: f5612b97c6942204776c3f69e048ecee58af9d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* spin_mutex.h                  -*-C++-*-
 *
 *************************************************************************
 *
 *  @copyright
 *  Copyright (C) 2009-2011
 *  Intel Corporation
 *  
 *  @copyright
 *  This file is part of the Intel Cilk Plus Library.  This library is free
 *  software; you can redistribute it and/or modify it under the
 *  terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3, or (at your option)
 *  any later version.
 *  
 *  @copyright
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  @copyright
 *  Under Section 7 of GPL version 3, you are granted additional
 *  permissions described in the GCC Runtime Library Exception, version
 *  3.1, as published by the Free Software Foundation.
 *  
 *  @copyright
 *  You should have received a copy of the GNU General Public License and
 *  a copy of the GCC Runtime Library Exception along with this program;
 *  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 *  <http://www.gnu.org/licenses/>.
 **************************************************************************/

/**
 * @file spin_mutex.h
 *
 * @brief Support for Cilk runtime mutexes.
 *
 * Cilk runtime mutexes are implemented as simple spin loops.
 *
 * This file is similar to a worker_mutex, except it does not have an
 * owner field.
 *
 * TBD: This class, worker_mutex, and os_mutex overlap quite a bit in
 * functionality.  Can we unify these mutexes somehow?
 */
#ifndef INCLUDED_SPIN_MUTEX_DOT_H
#define INCLUDED_SPIN_MUTEX_DOT_H

#include <cilk/common.h>
#include "rts-common.h"
#include "cilk_malloc.h"

__CILKRTS_BEGIN_EXTERN_C

/**
 * Mutexes are treated as an abstract data type within the Cilk
 * runtime system.  They are implemented as simple spin loops.
 */
typedef struct spin_mutex {
    /** Mutex spin loop variable. 0 if unowned, 1 if owned. */
    volatile int lock;

    /** Padding so the mutex takes up a cache line. */
    char pad[64/sizeof(int) - 1];
} spin_mutex;


/**
 * @brief Create a new Cilk spin_mutex.
 *
 * @return Returns an initialized spin mutex.  
 */
COMMON_PORTABLE
spin_mutex* spin_mutex_create();

/**
 * @brief Initialize a Cilk spin_mutex.
 *
 * @param m Spin_Mutex to be initialized.
 */
COMMON_PORTABLE
void spin_mutex_init(spin_mutex *m);

/**
 * @brief Acquire a Cilk spin_mutex.
 *
 * If statistics are being gathered, the time spent
 * acquiring the spin_mutex will be attributed to the specified worker.
 *
 * @param m Spin_Mutex to be initialized.
 */
COMMON_PORTABLE
void spin_mutex_lock(struct spin_mutex *m);
/**
 * @brief Attempt to lock a Cilk spin_mutex and fail if it isn't available.
 *
 * @param m Spin_Mutex to be acquired.
 *
 * @return 1 if the spin_mutex was acquired.
 * @return 0 if the spin_mutex was not acquired.
 */
COMMON_PORTABLE
int spin_mutex_trylock(struct spin_mutex *m);

/**
 * @brief Release a Cilk spin_mutex.
 *
 * @param m Spin_Mutex to be released.
 */
COMMON_PORTABLE
void spin_mutex_unlock(struct spin_mutex *m);

/**
 * @brief Deallocate a Cilk spin_mutex.  Currently does nothing.
 *
 * @param m Spin_Mutex to be deallocated.
 */
COMMON_PORTABLE
void spin_mutex_destroy(struct spin_mutex *m);

__CILKRTS_END_EXTERN_C

#endif // ! defined(INCLUDED_SPIN_MUTEX_DOT_H)