/*
 * remote_sem.h - POSIX unnammed semaphore definition.
 * 
 * Author   Alain Greiner    (2016)
 *
 * Copyright (c)  UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _SEMAPHORE_H_
#define _SEMAPHORE_H_

#include <hal_kernel_types.h>
#include <xlist.h>
#include <remote_spinlock.h>

/*********************************************************************************************
 *      This file defines a POSIX compliant unnamed semaphore.
 *
 * A semaphore is declared by a given user process as a "sem_t" global variable.
 * The user "sem_t" structure is implemented as an unsigned long, but the value is not
 * used by the kernel. ALMOS-MKH uses only the sem_t virtual address as an identifier.
 * For each user semaphore, ALMOS-MKH creates a kernel "remote_sem_t" structure.
 * - This structure is allocated in the reference cluster by the sem_init() syscall, 
 *   and destroyed by the sem_destroy() syscall, using RPC if the calling thread is not
 *   running in the reference cluster.
 * - The threads accessing the semaphore with the sem_get_value(), sem_wait(), sem_post()
 *   syscalls can be running in any cluster, as these syscalls access the "remote_sem_t"
 *   structure with remote_read / remote_write access functions.
 ********************************************************************************************/


/*********************************************************************************************
 * This structure defines the kernel semaphore descriptor.
 * - It contains the root of a waiting threads queue, implemented as a distributed xlist.
 * - It contains an xlist of all semaphores, rooted in the reference process descriptor.
 * - It contains a lock protecting both the semaphore value and the waiting queue.
 ********************************************************************************************/

typedef struct remote_sem_s
{
	remote_spinlock_t lock;          /*! lock protecting both count and wait_queue          */
	uint32_t          count;         /*! current value                                      */
    intptr_t          ident;         /*! virtual address in user space == identifier        */
    xlist_entry_t     root;          /*! root of waiting thread queue                       */
    xlist_entry_t     list;          /*! member of list of semaphores in same process       */
}
remote_sem_t;


/*********************************************************************************************
 * This function returns an extended pointer on the remote semaphore identified
 * by its virtual address in a given user process. It makes an associative search, 
 * scanning the list of semaphores rooted in the reference process descriptor.
 *********************************************************************************************
 * @ process  : pointer on local process descriptor.
 * @ vaddr    : semaphore virtual address, used as identifier.
 * @ returns extended pointer on semaphore if success / returns XPTR_NULL if not found. 
 ********************************************************************************************/
xptr_t remote_sem_from_vaddr( intptr_t  vaddr );

/*********************************************************************************************
 * This function implements the SEM_INIT operation.
 * It allocates memory for a remote semaphore in reference cluster, using a RPC if required, 
 * and initializes it, using remote accesses from values defined by the <vaddr> and <value>
 * arguments. It uses also a remote access to return the extended pointer on the semaphore 
 * in the buffer identified by the <buf_xp> argument.
 *********************************************************************************************
 * @ vaddr     : [in] semaphore virtual address, used as identifier.
 * @ value     : [in] semaphore initial value.
 * @ sem_xp_xp : [out] extended pointer on buffer to store extended pointer on semaphore.
 * @ returns 0 if success / returns -1 if no memory. 
 ********************************************************************************************/
error_t remote_sem_create( intptr_t  vaddr,
                           uint32_t  value,
                           xptr_t    sem_xp_xp );
 
/****************************yy***************************************************************
 * This function implements the SEM_DESTROY operation.
 * It desactivates the semaphore, and releases the physical memory allocated in the 
 * reference cluster, using a RPC if required.
 *********************************************************************************************
 * @ sem_xp   : [in] extended pointer on semaphore. 
 ********************************************************************************************/
void remote_sem_destroy( xptr_t sem_xp );

/****************************yy***************************************************************
 * This blocking function implements the SEM_WAIT operation.
 * - It returns only when the remote semaphore has a non-zero value,
 *   and has been atomically decremented.
 * -  if the semaphore has a zero value, it register the calling thread in the semaphore
 *    waiting queue, block the thread, and yield.
 *********************************************************************************************
 * @ sem_xp   : [in] extended pointer on semaphore. 
 ********************************************************************************************/
void remote_sem_wait( xptr_t sem_xp );

/****************************yy***************************************************************
 * This function mplements the SEM_POST operation.
 * - It atomically increments the remote semaphore if the semaphore waiting queue is empty. 
 * - If the waiting queue is not empty, it wakes up the first waiting thread.
 *********************************************************************************************
 * @ sem_xp   : [in] extended pointer on semaphore. 
 ********************************************************************************************/
void remote_sem_post( xptr_t sem_xp );

/****************************yy***************************************************************
 * This function implements the SEM_GETVALUE operation.
 * It returns in the <data> buffer the semaphore current value. 
 *********************************************************************************************
 * @ sem_xp   : [in]  extended pointer on semaphore. 
 * @ data     : [out] local pointer on buffer for returned value.
 ********************************************************************************************/
void remote_sem_get_value( xptr_t      sem_xp,
                           uint32_t  * data );


#endif	/* _SEMAPHORE_H_ */
