summaryrefslogtreecommitdiff
path: root/hurd/lock.h
blob: 18aacfb8ed60e431b904b186b155510f568f080e (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* lock.c - Locking helper functions.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   GNU Hurd is free software: you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   GNU Hurd 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with GNU Hurd.  If not, see
   <http://www.gnu.org/licenses/>.  */

#ifndef _HURD_LOCK_H
#define _HURD_LOCK_H

#include <l4/thread.h>
#include <l4/ipc.h>
#include <assert.h>

#ifndef NDEBUG
#include <hurd/stddef.h>

struct ss_lock_trace
{
  const char *caller;
  unsigned int line : 28;
  unsigned int func : 4;
  void *lock;
  l4_thread_id_t tid;
};
#define SS_LOCK_TRACE_COUNT 1000
extern struct ss_lock_trace ss_lock_trace[SS_LOCK_TRACE_COUNT];
extern int ss_lock_trace_count;
#endif /* NDEBUG */

enum
  {
    SS_MUTEX_LOCK,
    SS_MUTEX_LOCK_WAIT,
    SS_MUTEX_UNLOCK,
    SS_MUTEX_TRYLOCK,
    SS_MUTEX_TRYLOCK_BLOCKED,

    SS_RMUTEX_LOCK,
    SS_RMUTEX_LOCK_INC,
    SS_RMUTEX_LOCK_WAIT,
    SS_RMUTEX_UNLOCK,
    SS_RMUTEX_UNLOCK_DEC,
    SS_RMUTEX_TRYLOCK,
    SS_RMUTEX_TRYLOCK_INC,
    SS_RMUTEX_TRYLOCK_BLOCKED,
  };

static inline void
ss_lock_trace_dump (void *lock)
{
#ifndef NDEBUG
  int c = 0;
  int i;
  for (i = 0; i < SS_LOCK_TRACE_COUNT; i ++)
    {
      struct ss_lock_trace *trace
	= &ss_lock_trace[(ss_lock_trace_count - i - 1) % SS_LOCK_TRACE_COUNT];

      if (! trace->func)
	break;

      if (trace->lock != lock)
	continue;

      __const char *func = 0;
      switch (trace->func)
	{
	case SS_MUTEX_LOCK:
	  func = "lock";
	  break;
	case SS_MUTEX_LOCK_WAIT:
	  func = "lock(wait)";
	  break;
	case SS_MUTEX_UNLOCK:
	  func = "unlock";
	  break;
	case SS_MUTEX_TRYLOCK:
	  func = "trylock";
	  break;
	case SS_MUTEX_TRYLOCK_BLOCKED:
	  func = "trylock(blocked)";
	  break;

	case SS_RMUTEX_LOCK:
	  func = "ss_rmutex_lock";
	  break;
	case SS_RMUTEX_LOCK_INC:
	  func = "ss_rmutex_lock_inc";
	  break;
	case SS_RMUTEX_LOCK_WAIT:
	  func = "ss_rmutex_lock_wait";
	  break;
	case SS_RMUTEX_UNLOCK:
	  func = "ss_rmutex_unlock";
	  break;
	case SS_RMUTEX_UNLOCK_DEC:
	  func = "ss_rmutex_unlock_dec";
	  break;
	case SS_RMUTEX_TRYLOCK:
	  func = "ss_rmutex_trylock";
	  break;
	case SS_RMUTEX_TRYLOCK_INC:
	  func = "ss_rmutex_trylock_inc";
	  break;
	case SS_RMUTEX_TRYLOCK_BLOCKED:
	  func = "ss_rmutex_trylock_blocked";
	  break;
	}
      assert (func);

      S_PRINTF ("%d: %s(%p) from %s:%d by %x\n",
		c --, func, lock, trace->caller, trace->line, trace->tid);
      if (c < -30)
	/* Show about the last 30 transactions.  */
	break;
    }
#endif  /* NDEBUG */
}

static inline void
ss_mutex_trace_add (int func, __const char *caller, int line, void *lock)
{
#ifndef NDEBUG
  int i = (ss_lock_trace_count ++) % SS_LOCK_TRACE_COUNT;

  ss_lock_trace[i].func = func;
  ss_lock_trace[i].caller = caller;
  ss_lock_trace[i].line = line;
  ss_lock_trace[i].lock = lock;
  ss_lock_trace[i].tid = l4_myself ();
#endif  /* NDEBUG */
}

/* Wait for a wakeup message from FROM.  */
static inline void
__ss_lock_wait (l4_thread_id_t from)
{
  /* We didn't get the lock.  */
  l4_accept (L4_UNTYPED_WORDS_ACCEPTOR);
  l4_msg_tag_t tag = l4_receive (from);
  assertx (! l4_ipc_failed (tag),
	   "Waiting on %x failed: %d (offset: %x)",
	   from, (l4_error_code () >> 1) & 0x7, l4_error_code () >> 4);
}

/* Wake up target TARGET.  */
static inline void
__ss_lock_wakeup (l4_thread_id_t target)
{
  l4_msg_t msg;
  l4_msg_clear (msg);
  l4_msg_set_untyped_words (msg, 0);
  l4_msg_load (msg);
  l4_msg_tag_t tag = l4_send (target);
  assertx (! l4_ipc_failed (tag),
	   "Waking %x failed: %d (offset: %x)",
	   target, (l4_error_code () >> 1) & 0x7, l4_error_code () >> 4);
}

#endif /* _HURD_LOCK_H  */