// SPDX-License-Identifier: Apache-2.0
/*
 * Copyright 2001-2026 Richard Lesh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <system/system.def>

// SpinLock structure is just a single atomic boolean
#def_macro  DEFINE_SPINLOCK(name)
    #global ${name}:   .dci    0
#end_macro

#def_macro  DEFINE_SHARED_SPINLOCK(name)
    #global ${name}:   .dci    0
	move	r1, 1
	int		iALLOC_SHARED
	store	r0, ${name}
#end_macro

// Recursive SpinLock structure is:
// 0: PID that owns spinlock or 0
// 1: Re-entry count, decrement to 0 releases lock
#def_macro  DEFINE_RECURSIVE_SPINLOCK(name)
    #global ${name}:   .dca    1
#end_macro

#def_macro  DEFINE_SHARED_RECURSIVE_SPINLOCK(name)
    #global ${name}:   .dci    0
	move	r1, 2
	int		iALLOC_SHARED
	store	r0, ${name}
#end_macro

// Mutex structure is:
// 0: PID that owns mutex or 0
// 1: Re-entry count, decrement to 0 releases lock
// 2: Heap allocated array of waiting PIDs
// 3: SpinLock to protect array of waiting PIDs

#define		MUTEX_SIZE	4

#def_macro  DEFINE_MUTEX(name)
    #global ${name}:   .dca    4 - 1
#end_macro

#def_macro  DEFINE_SHARED_MUTEX(name)
    #global ${name}:   .dcw    0
	move	r1, 4
	int		iALLOC_SHARED
	store	r0, ${name}
#end_macro

// Returns thread ID of the new thread in R0
#def_macro	create_thread(entryPoint, data)
	move	r1, ${entryPoint}
	move	r2, ${data}
	int		iTHREAD
#end_macro

#def_macro	join_thread(threadPID)
	move	r1, ${threadPID}
	int		iJOIN_THREAD
#end_macro

#def_macro	sleep(delay_in_milliseconds)
	move	r1, ${delay_in_milliseconds}
	int		iSLEEP
#end_macro

#def_macro	wake_thread(threadPID)
	move	r1, ${threadPID}
	int		iWAKE_THREAD
#end_macro
