summaryrefslogtreecommitdiff
path: root/eventflag.c
blob: 27714a16223ddcda5e5effd91adab474ee4ae129 (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
/*
 * eventflag.c - Eventflag with/without timeout
 *
 * Copyright (C) 2013 Flying Stone Technology
 * Author: NIIBE Yutaka <gniibe@fsij.org>
 *
 * This file is a part of Chopstx, a thread library for embedded.
 *
 * Chopstx 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 of the License, or
 * (at your option) any later version.
 *
 * Chopstx 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As additional permission under GNU GPL version 3 section 7, you may
 * distribute non-source form of the Program without the copy of the
 * GNU GPL normally required by section 4, provided you inform the
 * receipents of GNU GPL by a written offer.
 *
 */

#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include <eventflag.h>

enum {
  EVENTFLAG_ERR_WAIT = CHOPSTX_ERR_JOIN + 1,
  EVENTFLAG_ERR_TIMED_WAIT,
};


void
eventflag_init (struct eventflag *ev, chopstx_t sleeper)
{
  ev->sleeper = sleeper;

  if (sleeper)
    ev->u.wait_usec = 0;
  else
    chopstx_cond_init (&ev->u.cond);

  ev->flag = 0;
  chopstx_mutex_init (&ev->mutex);
}


eventmask_t
eventflag_wait (struct eventflag *ev)
{
  int n;

  if (ev->sleeper)
    chx_fatal (EVENTFLAG_ERR_WAIT);

  chopstx_mutex_lock (&ev->mutex);
  if (!ev->flag)
    chopstx_cond_wait (&ev->u.cond, &ev->mutex);

  n = __builtin_ffs (ev->flag);
  ev->flag &= ~(1 << (n - 1));
  chopstx_mutex_unlock (&ev->mutex);

  return (1 << (n - 1));
}

eventmask_t
eventflag_wait_timeout (struct eventflag *ev, uint32_t usec)
{
  eventmask_t em = 0;
  int n;

  if (ev->sleeper == 0)
    chx_fatal (EVENTFLAG_ERR_TIMED_WAIT);

  chopstx_mutex_lock (&ev->mutex);

  if (!ev->flag)
    {
      ev->u.wait_usec = usec;
      chopstx_mutex_unlock (&ev->mutex);
      chopstx_usec_wait_var (&ev->u.wait_usec);
      chopstx_mutex_lock (&ev->mutex);
      ev->u.wait_usec = 0;
    }

  n = __builtin_ffs (ev->flag);
  if (n)
    {
      em = (1 << (n - 1));
      ev->flag &= ~em;
    }

  chopstx_mutex_unlock (&ev->mutex);
  return em;
}


void
eventflag_signal (struct eventflag *ev, eventmask_t m)
{
  chopstx_mutex_lock (&ev->mutex);
  ev->flag |= m;
  if (ev->sleeper)
    {
      if (ev->u.wait_usec)
	{
	  ev->u.wait_usec = 0;
	  chopstx_wakeup_usec_wait (ev->sleeper);
	}
    }
  else
    chopstx_cond_signal (&ev->u.cond);
  chopstx_mutex_unlock (&ev->mutex);
}