blob: 6694d0019a682dfdf8cc62257b3597958dd49d26 [file] [log] [blame]
Thomas Gleixner3e456102019-06-01 10:08:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Matthew Wilcox64ac24e2008-03-07 21:55:58 -05002/*
3 * Copyright (c) 2008 Intel Corporation
4 * Author: Matthew Wilcox <willy@linux.intel.com>
5 *
Tycho Andersen2dd6fd22018-02-01 12:41:19 +01006 * Please see kernel/locking/semaphore.c for documentation of these functions
Matthew Wilcox64ac24e2008-03-07 21:55:58 -05007 */
8#ifndef __LINUX_SEMAPHORE_H
9#define __LINUX_SEMAPHORE_H
10
11#include <linux/list.h>
12#include <linux/spinlock.h>
13
Matthew Wilcox714493c2008-04-11 15:23:52 -040014/* Please don't access any members of this structure directly */
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050015struct semaphore {
Thomas Gleixner8292c9e12010-02-24 09:50:22 +010016 raw_spinlock_t lock;
Matthew Wilcoxb17170b2008-03-14 14:35:22 -040017 unsigned int count;
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050018 struct list_head wait_list;
19};
20
21#define __SEMAPHORE_INITIALIZER(name, n) \
22{ \
Thomas Gleixner8292c9e12010-02-24 09:50:22 +010023 .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050024 .count = n, \
25 .wait_list = LIST_HEAD_INIT((name).wait_list), \
26}
27
Thomas Gleixnerfebc88c2010-09-07 14:46:37 +020028#define DEFINE_SEMAPHORE(name) \
29 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
30
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050031static inline void sema_init(struct semaphore *sem, int val)
32{
33 static struct lock_class_key __key;
34 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
35 lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
36}
37
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050038extern void down(struct semaphore *sem);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050039extern int __must_check down_interruptible(struct semaphore *sem);
Matthew Wilcoxf06d9682008-03-14 13:19:33 -040040extern int __must_check down_killable(struct semaphore *sem);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050041extern int __must_check down_trylock(struct semaphore *sem);
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040042extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050043extern void up(struct semaphore *sem);
44
45#endif /* __LINUX_SEMAPHORE_H */