/*
 * lock.h
 *
 * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _LOCK_H_
#define _LOCK_H_

#include <sdk/defs.h>

#define DECLARE_LOCK(x) lock_t x = {0}

typedef uint8_t critical_t;

typedef struct
{
    uintptr_t holder;
    int32_t count;
} lock_t;

static inline void lock_init(lock_t *lock)
{
    lock->holder = lock->count = 0;
}

void enter_critical(critical_t *critical);
void leave_critical(critical_t *critical);
void lock_acquire(lock_t *lock);
void lock_acquire_smart(lock_t *lock);
void lock_acquire_shared(lock_t *lock);
void lock_release(lock_t *lock);

#endif
