1/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2/*
3 * See COPYRIGHT in top-level directory.
4 */
5
6#ifndef _ZM_LOCK_TYPES_H
7#define _ZM_LOCK_TYPES_H
8
9#if !defined(_GNU_SOURCE)
10#define _GNU_SOURCE
11#endif
12#include <stdlib.h>
13#include <assert.h>
14#include <errno.h>
15#include <pthread.h>
16#include "common/zm_common.h"
17
18#define ZM_LOCKED 1
19#define ZM_UNLOCKED 0
20
21typedef struct zm_ticket zm_ticket_t;
22
23struct zm_ticket {
24    zm_atomic_uint_t next_ticket;
25    zm_atomic_uint_t now_serving;
26};
27
28/* MCS */
29typedef zm_ptr_t zm_mcs_t;
30typedef struct zm_mcs_qnode zm_mcs_qnode_t;
31struct zm_mcs_qnode {
32    zm_atomic_uint_t status;
33    zm_atomic_ptr_t next;
34};
35
36
37/* Context Saving MCS */
38typedef struct zm_mmcs zm_mmcs_t;
39
40struct zm_mmcs {
41    zm_atomic_ptr_t lock;
42    zm_mcs_qnode_t* cur_ctx __attribute__((aligned(64)));
43};
44
45/* HMCS */
46
47typedef zm_ptr_t zm_hmcs_t;
48
49/* Two-Level Priority */
50#define ZM_TICKET   1
51#define ZM_MCS      2
52#define ZM_HMCS     3
53
54#define ZM_TLP_HIGH_P @ZM_TLP_HIGH_P@
55#define ZM_TLP_LOW_P @ZM_TLP_LOW_P@
56
57#define ZM_STATUS_FREE    0
58#define ZM_STATUS_REQUEST 1
59#define ZM_STATUS_GRANTED 2
60
61typedef struct zm_tlp zm_tlp_t;
62
63struct zm_tlp {
64#if (ZM_TLP_HIGH_P == ZM_TICKET)
65   zm_ticket_t high_p __attribute__((aligned(64)));
66#elif (ZM_TLP_HIGH_P == ZM_MCS)
67   zm_mcs_t high_p __attribute__((aligned(64)));
68#elif (ZM_TLP_HIGH_P == ZM_HMCS)
69   zm_hmcs_t high_p __attribute__((aligned(64)));
70#endif
71   int go_straight;
72   int low_p_acq __attribute__((aligned(64)));
73   zm_ticket_t filter __attribute__((aligned(64)));
74#if (ZM_TLP_LOW_P == ZM_TICKET)
75   zm_ticket_t low_p __attribute__((aligned(64)));
76#elif (ZM_TLP_LOW_P == ZM_MCS)
77   zm_mcs_t low_p __attribute__((aligned(64)));
78#elif (ZM_TLP_LOW_P == ZM_HMCS)
79   zm_hmcs_t low_p __attribute__((aligned(64)));
80#endif
81};
82
83typedef struct zm_mcsp zm_mcsp_t;
84
85struct zm_mcsp {
86    zm_mcs_t high_p __attribute__((aligned(64)));
87    int go_straight;
88    int low_p_acq __attribute__((aligned(64)));
89    zm_ticket_t filter __attribute__((aligned(64)));
90    zm_mcs_t low_p __attribute__((aligned(64)));
91};
92
93#include "cond/zm_cond_types.h"
94struct zm_hmpr_pnode {
95    unsigned p; /* priority */
96    zm_mcs_qnode_t *qnode;
97};
98
99struct zm_hmpr {
100    zm_hmcs_t lock;
101    int go_straight;
102    zm_mcs_qnode_t znode;
103    int low_p_acq __attribute__((aligned(64)));
104    zm_mcs_t waitq __attribute__((aligned(64)));
105};
106
107#endif /* _IZEM_LOCK_TYPES_H */
108