xref: /netbsd-src/lib/libpthread/pthread_getcpuclockid.c (revision 7adb41074dac288e339b9f066df80fad4df5817f)
1*7adb4107Sriastradh /*	$NetBSD: pthread_getcpuclockid.c,v 1.4 2022/02/12 14:59:32 riastradh Exp $	*/
2d394d2a9Schristos 
3d394d2a9Schristos /*-
4d394d2a9Schristos  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5d394d2a9Schristos  * All rights reserved.
6d394d2a9Schristos  *
7d394d2a9Schristos  * This code is derived from software contributed to The NetBSD Foundation
8d394d2a9Schristos  * by Christos Zoulas.
9d394d2a9Schristos  *
10d394d2a9Schristos  * Redistribution and use in source and binary forms, with or without
11d394d2a9Schristos  * modification, are permitted provided that the following conditions
12d394d2a9Schristos  * are met:
13d394d2a9Schristos  * 1. Redistributions of source code must retain the above copyright
14d394d2a9Schristos  *    notice, this list of conditions and the following disclaimer.
15d394d2a9Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16d394d2a9Schristos  *    notice, this list of conditions and the following disclaimer in the
17d394d2a9Schristos  *    documentation and/or other materials provided with the distribution.
18d394d2a9Schristos  *
19d394d2a9Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d394d2a9Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d394d2a9Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d394d2a9Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d394d2a9Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d394d2a9Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d394d2a9Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d394d2a9Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d394d2a9Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d394d2a9Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d394d2a9Schristos  * POSSIBILITY OF SUCH DAMAGE.
30d394d2a9Schristos  */
31d394d2a9Schristos #include <sys/cdefs.h>
32d394d2a9Schristos #if defined(LIBC_SCCS) && !defined(lint)
33*7adb4107Sriastradh __RCSID("$NetBSD: pthread_getcpuclockid.c,v 1.4 2022/02/12 14:59:32 riastradh Exp $");
34d394d2a9Schristos #endif /* LIBC_SCCS and not lint */
35d394d2a9Schristos 
36*7adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
37*7adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
38*7adb4107Sriastradh 
39d394d2a9Schristos #include <sys/types.h>
405e0724b3Snjoly #include <errno.h>
41d394d2a9Schristos #include <pthread.h>
42d394d2a9Schristos #include <time.h>
43d394d2a9Schristos 
44d394d2a9Schristos #include "pthread_int.h"
45d394d2a9Schristos 
46d394d2a9Schristos int
pthread_getcpuclockid(pthread_t thread,clockid_t * clock_id)47d394d2a9Schristos pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id)
48d394d2a9Schristos {
495e0724b3Snjoly 	int error = 0, saved_errno;
505e0724b3Snjoly 
5120668e14Skamil 	pthread__error(EINVAL, "Invalid thread",
5220668e14Skamil 	    thread->pt_magic == PT_MAGIC);
5320668e14Skamil 
545e0724b3Snjoly 	saved_errno = errno;
555e0724b3Snjoly 	if (clock_getcpuclockid2(P_LWPID, (id_t)thread->pt_lid, clock_id) == -1)
565e0724b3Snjoly 		error = errno;
575e0724b3Snjoly 	errno = saved_errno;
585e0724b3Snjoly 
595e0724b3Snjoly 	return error;
60d394d2a9Schristos }
61