summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/KernelKit/Semaphore.h
blob: a00273e8f662a7da265d3d6c4b3d56b55f1a4b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* -------------------------------------------

  Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.

------------------------------------------- */

#pragma once

/// @author Amlal El Mahrouss
/// @file Semaphore.h
/// @brief Semaphore structure and functions for synchronization in the kernel.

#include <CompilerKit/CompilerKit.h>
#include <KernelKit/Timer.h>
#include <NeKit/Defines.h>

#define kSemaphoreOwnerIndex (0)
#define kSemaphoreCountIndex (1)

#define kSemaphoreCount (2)

#define kSemaphoreIncremenntOwner(sem) \
  (sem[kSemaphoreOwnerIndex]++)


#define kSemaphoreDecrementOwner(sem) \
  (sem[kSemaphoreOwnerIndex]--)


namespace Kernel {
/// @brief Semaphore structure used for synchronization.
typedef UInt64 Semaphore[kSemaphoreCount];

/// @brief Checks if the semaphore is valid.
inline BOOL rtl_sem_is_valid(const Semaphore& sem, UInt64 owner = 0) {
  return sem[kSemaphoreOwnerIndex] != owner || sem[kSemaphoreCountIndex] > 0;
}

/// @brief Releases the semaphore, resetting its owner and count.
/// @param sem
/// @return
inline BOOL rtl_sem_release(Semaphore& sem) {
  sem[kSemaphoreOwnerIndex] = 0;
  sem[kSemaphoreCountIndex] = 0;

  return TRUE;
}

/// @brief Initializes the semaphore with an owner and a count of zero.
/// @param sem
/// @param owner
/// @return
inline BOOL rtl_sem_acquire(Semaphore& sem, UInt64 owner) {
  if (!owner) {
    err_global_get() = kErrorInvalidData;
    return FALSE;  // Invalid owner
  }

  sem[kSemaphoreOwnerIndex] = owner;
  sem[kSemaphoreCountIndex] = 0;

  return TRUE;
}

/// @brief Waits for the semaphore to be available, blocking until it is.
/// @param sem
/// @param timeout
/// @return
inline BOOL rtl_sem_wait(Semaphore& sem, UInt64 owner, UInt64 timeout, BOOL* condition = nullptr) {
  if (!rtl_sem_is_valid(sem, owner)) {
    return FALSE;
  }

  if (timeout <= 0) {
    err_global_get() = kErrorNetworkTimeout;

    return FALSE;
  }

  if (!condition || *condition) {
    err_global_get() = kErrorSuccess;
    sem[kSemaphoreCountIndex]--;

    return TRUE;
  }

  HardwareTimer timer(timeout);
  BOOL          ret = timer.Wait();

  if (ret) {
    if (!condition || *condition) {
      err_global_get() = kErrorSuccess;
      sem[kSemaphoreCountIndex]--;

      return TRUE;
    }
  }

  err_global_get() = kErrorNetworkTimeout;

  return FALSE;  // Failed to acquire semaphore
}  // namespace Kernel
}  // namespace Kernel