sync.h revision 3c6bf30f1e520b250242da39a493986b8b2cef53
1/*
2 * sync.h
3 *
4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5 *
6 * Provide synchronization services.
7 *
8 * Copyright (C) 2005-2006 Texas Instruments, Inc.
9 *
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 */
18
19#ifndef _SYNC_H
20#define _SYNC_H
21
22#include <dspbridge/dbdefs.h>
23#include <dspbridge/host_os.h>
24
25
26/* Special timeout value indicating an infinite wait: */
27#define SYNC_INFINITE  0xffffffff
28
29/**
30 * struct sync_object - the basic sync_object structure
31 * @comp:	use to signal events
32 * @multi_comp:	use to signal multiple events.
33 *
34 */
35struct sync_object{
36	struct completion comp;
37	struct completion *multi_comp;
38};
39
40/**
41 * sync_init_event() - set initial state for a sync_event element
42 * @event:	event to be initialized.
43 *
44 * Set the initial state for a sync_event element.
45 */
46
47static inline void sync_init_event(struct sync_object *event)
48{
49	init_completion(&event->comp);
50	event->multi_comp = NULL;
51}
52
53/**
54 * sync_reset_event() - reset a sync_event element
55 * @event:	event to be reset.
56 *
57 * This function reset to the initial state to @event.
58 */
59
60static inline void sync_reset_event(struct sync_object *event)
61{
62	INIT_COMPLETION(event->comp);
63	event->multi_comp = NULL;
64}
65
66/**
67 * sync_set_event() - set or signal and specified event
68 * @event:	Event to be set..
69 *
70 * set the @event, if there is an thread waiting for the event
71 * it will be waken up, this function only wakes one thread.
72 */
73
74void sync_set_event(struct sync_object *event);
75
76/**
77 * sync_wait_on_event() - waits for a event to be set.
78 * @event:	events to wait for it.
79 * @timeout	timeout on waiting for the evetn.
80 *
81 * This functios will wait until @event is set or until timeout. In case of
82 * success the function will return 0 and
83 * in case of timeout the function will return -ETIME
84 * in case of signal the function will return -ERESTARTSYS
85 */
86
87static inline int sync_wait_on_event(struct sync_object *event,
88							unsigned timeout)
89{
90	int res;
91
92	res = wait_for_completion_interruptible_timeout(&event->comp,
93						msecs_to_jiffies(timeout));
94	if (!res)
95		res = -ETIME;
96	else if (res > 0)
97		res = 0;
98
99	return res;
100}
101
102/**
103 * sync_wait_on_multiple_events() - waits for multiple events to be set.
104 * @events:	Array of events to wait for them.
105 * @count:	number of elements of the array.
106 * @timeout	timeout on waiting for the evetns.
107 * @pu_index	index of the event set.
108 *
109 * This functios will wait until any of the array element is set or until
110 * timeout. In case of success the function will return 0 and
111 * @pu_index will store the index of the array element set and in case
112 * of timeout the function will return -ETIME.
113 */
114
115int sync_wait_on_multiple_events(struct sync_object **events,
116				     unsigned count, unsigned timeout,
117				     unsigned *index);
118
119#endif /* _SYNC_H */
120