Encapsulation class for System V named semaphores, used for process/thread mutex

Source: Internet
Author: User
Recently, in order to access resources mutex by multiple processes, the System V named semaphore is used. For convenience, C ++ is encapsulated. The Code is as follows.
Aoslock. HPP:
# Ifndef _ aoslock_hpp
# DEFINE _ aoslock_hpp

# Include <sys/types. h>
# Include <sys/IPC. h>
# Include <sys/SEM. h>
# Include <stdio. h>
# Include <errno. h>
# Include <stdlib. h>
# Include <fcntl. h>

Typedef Enum
{
Sem_fail =-1,
Sem_ OK,
Sem_busy
} Sem_ret_value;

Union semun
{
Int val;
Struct semid_ds * Buf;
};

Class aoslock
{
PRIVATE:
Int Semid _;
Key_t key _;
Int resnum _;

Public:
Aoslock ();
Aoslock (const char * keypathname );
Int initiallock (INT resnum );
Int getval ();
Void enter1 ();
Void leave1 ();
Void entern (INT resnum );
Void leaven (INT resnum );

Int entern_no_wait (INT resnum); // return sem_busy or sem_ OK or sem_fail
Void leaven_no_wait (INT resnum );
Void releaselock ();
};
# Endif

Aoslock. cpp:
# Include "aoslock. HPP"

Aoslock: aoslock ()
{
Key _ = ftok ("/root", 1 );
}

Aoslock: aoslock (const char * keypathname)
{
Key _ = ftok (keypathname, 1 );
}

Int aoslock: initiallock (INT resnum)
{
Semid _ = semget (Key _, 1, 0666 | ipc_creat | ipc_excl );
// Printf ("SEM id = % d/N", Semid _);
If (Semid _ <0)
{
// Perror ("semget ");
// Int tmperrno = errno;
// Printf ("errno = % d, expected = % d/N", errno, eexist );
// If (tmperrno = eexist)
{
// Printf ("exist/N ");
Semid _ = semget (Key _, 1, 0666 | ipc_creat );
}
}
Else
{
// Printf ("First time/N ");
Resnum _ = resnum;
Union semun ARG;
Arg. Val = resnum;
If (semctl (Semid _, 0, setval, ARG) <0)
{
// Perror ("semctl ");
}
}
// Printf ("sem id _ = % d/N", Semid _);
/* Int value = semctl (Semid _, 0, getval );
If (value <0)
{
Perror ("semctl ");
}
Printf ("SEM value = % d/N", value );*/

Return Semid _;
}

Int aoslock: getval ()
{
Int value = semctl (Semid _, 0, getval );
If (value <0)
{
// Perror ("semctl ");
}
// Printf ("SEM value = % d/N", value );
Return value;
}

Void aoslock: enter1 ()
{
Struct sembuf p_buf;
P_buf.sem_num = 0;
P_buf.sem_op =-1;
P_buf.sem_flg = sem_undo;
Semop (Semid _, & p_buf, 1 );
}

Void aoslock: leave1 ()
{
Struct sembuf v_buf;
V_buf.sem_num = 0;
V_buf.sem_op = 1;
V_buf.sem_flg = sem_undo;
Semop (Semid _, & v_buf, 1 );
}

Void aoslock: entern (INT resnum)
{
Struct sembuf p_buf;
P_buf.sem_num = 0;
P_buf.sem_op =-resnum;
P_buf.sem_flg = sem_undo;
Semop (Semid _, & p_buf, 1 );
}

Void aoslock: Leaven (INT resnum)
{
Struct sembuf v_buf;
V_buf.sem_num = 0;
V_buf.sem_op = resnum;
V_buf.sem_flg = sem_undo;
Semop (Semid _, & v_buf, 1 );
}

Void aoslock: releaselock ()
{
If (semctl (Semid _, 0, ipc_rmid) =-1)
Perror ("semctl ");
}

Int aoslock: entern_no_wait (INT resnum)
{
Struct sembuf p_buf;
P_buf.sem_num = 0;
P_buf.sem_op =-resnum;
P_buf.sem_flg = ipc_nowait;
If (semop (Semid _, & p_buf, 1) <0)
{
If (errno = eagain)
{
Return sem_busy;
}
Else
{
Return sem_fail;
}
}
Return sem_ OK;
}

Void aoslock: leaven_no_wait (INT resnum)
{
Struct sembuf v_buf;
V_buf.sem_num = 0;
V_buf.sem_op = resnum;
V_buf.sem_flg = ipc_nowait;
Semop (Semid _, & v_buf, 1 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.