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