summaryrefslogtreecommitdiff
path: root/libthreads/cancel-cond.c
blob: b7793f85d09f90037551cceea38ae50b8d3a3aeb (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
/* Modified condition_wait that checks for cancellation.
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
This file is part of the GNU C Library.

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

The GNU C 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA.  */

#include <hurd/signal.h>
#include <cthreads.h>
#include "cthread_internals.h"
#include <assert.h>

/* Just like condition_wait, but cancellable.  Returns true if cancelled.  */
int
hurd_condition_wait (condition_t c, mutex_t m)
{
  /* This function will be called by hurd_thread_cancel while we are blocked
     in the condition_wait.  We wake up all threads blocked on C,
     so our thread will wake up and notice the cancellation flag.  */
  void cancel_me (void)
    {
      condition_broadcast (c);
    }
  struct hurd_sigstate *ss = _hurd_self_sigstate ();
  cproc_t p = cproc_self ();
  int cancel;

  assert (ss->intr_port == MACH_PORT_NULL); /* Sanity check for signal bugs. */

  p->state = CPROC_CONDWAIT | CPROC_SWITCHING;

  /* Atomically enqueue our cproc on the condition variable's queue of
     waiters, and mark our sigstate to indicate that `cancel_me' must be
     called to wake us up.  We must hold the sigstate lock while acquiring
     the condition variable's lock and tweaking it, so that
     hurd_thread_cancel can never suspend us and then deadlock in
     condition_broadcast waiting for the condition variable's lock.  */

  spin_lock (&ss->lock);
  spin_lock (&c->lock);
  cancel = ss->cancel;
  if (cancel)
    /* We were cancelled before doing anything.  Don't block at all.  */
    ss->cancel = 0;
  else
    {
      /* Put us on the queue so that condition_broadcast will know to wake
         us up.  */
      cthread_queue_enq (&c->queue, p);
      /* Tell hurd_thread_cancel how to unblock us.  */
      ss->cancel_hook = &cancel_me;
    }
  spin_unlock (&c->lock);
  spin_unlock (&ss->lock);

  if (cancel)
    {
      /* Cancelled on entry.  Just leave the mutex locked.  */
      m = NULL;
      p->state = CPROC_RUNNING;
    }
  else
    {
      /* Now unlock the mutex and block until woken.  */

#ifdef	WAIT_DEBUG
      p->waiting_for = (char *)c;
#endif	 /* WAIT_DEBUG */

      mutex_unlock (m);

      spin_lock (&p->lock);
      if (p->state & CPROC_SWITCHING)
	cproc_block ();
      else
	{
	  /* We were woken up someplace before reacquiring P->lock.
	     We can just continue on.  */
	  p->state = CPROC_RUNNING;
	  spin_unlock(&p->lock);
	}

#ifdef	WAIT_DEBUG
      p->waiting_for = (char *)0;
#endif	 /* WAIT_DEBUG */
    }

  spin_lock (&ss->lock);
  /* Clear the hook, now that we are done blocking.  */
  ss->cancel_hook = NULL;
  /* Check the cancellation flag; we might have unblocked due to
     cancellation rather than a normal condition_signal or
     condition_broadcast (or we might have just happened to get cancelled
     right after waking up).  */
  cancel |= ss->cancel;
  ss->cancel = 0;
  spin_unlock (&ss->lock);

  if (m)
    /* Reacquire the mutex and return.  */
    mutex_lock (m);

  return cancel;
}