• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/12.2/sys/kern/kern_condvar.c 326271 2017-11-27 15:20:12Z pfg $");
31 
32 #include <sys/condvar.h>
33 #include <sys/callout.h>
34 #include <time.h>
35 #include <los_event.h>
36 
37 extern UINT32 OsEventWriteOnce(EVENT_CB_S *eventCb, UINT32 events);
38 
39 int
cv_timedwait(struct cv * cv,struct mtx * mtx,int tw_ms)40 cv_timedwait(struct cv *cv, struct mtx *mtx, int tw_ms)
41 {
42 	struct timespec abstime;
43 
44 	abstime.tv_sec = tw_ms / MSEC_PER_SEC;
45 	abstime.tv_nsec = (tw_ms%MSEC_PER_SEC) * NSEC_PER_MSEC;
46 	return (pthread_cond_timedwait(cv, mtx, &abstime));
47 }
48 
49 int
cv_signal(pthread_cond_t * cond)50 cv_signal(pthread_cond_t *cond)
51 {
52 	int ret;
53 
54 	if (cond == NULL) {
55 		return (EINVAL);
56 	}
57 	ret = OsEventWriteOnce(&(cond->event), 0x01);
58 
59 	if (ret != 0) {
60 		return (EINVAL);
61 	}
62 
63 	return (ret);
64 }
65 
66 int
cv_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)67 cv_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
68 {
69 	int ret;
70 
71 	if ((cond == NULL) || (mutex == NULL)) {
72 		return (EINVAL);
73 	}
74 
75 	(void)pthread_mutex_lock(cond->mutex);
76 	if (cond->value == -1) {
77 		cond->value = 0;
78 		ret = LOS_EventInit(&(cond->event));
79 		if (ret != LOS_OK) {
80 			(void)pthread_mutex_unlock(cond->mutex);
81 			return (EINVAL);
82 		}
83 	}
84 	(void)pthread_mutex_unlock(cond->mutex);
85 
86 	if (pthread_mutex_unlock(mutex) != ENOERR) {
87 		PRINTK("%s %d\n",__FUNCTION__,__LINE__);
88 	}
89 
90 	ret = LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
91 
92 	if (pthread_mutex_lock(mutex) != ENOERR) {
93 		PRINTK("%s %d\n",__FUNCTION__,__LINE__);
94 	}
95 
96 	return (ret);
97 }
98 
99 int
cv_broadcast(pthread_cond_t * cond)100 cv_broadcast(pthread_cond_t *cond)
101 {
102 	int ret;
103 
104 	if (cond == NULL) {
105 		return (EINVAL);
106 	}
107 
108 	ret = LOS_EventWrite(&(cond->event), 0x01);
109 	if (ret != 0) {
110 		return (EINVAL);
111 	}
112 
113 	return (ret);
114 }
115