Next Previous Contents

18. Context lock: multi-user support

#Specification: CONTEXT_LOCK object / principles ([,14])

The CONTEXT_LOCK object provides a way to lock some area in Linuxconf. This is fully multi-user and simple to use. Ideally, every module should provides some locking to prevent two Linuxconf instance from entering at once. The class provides support for both global and fine grain locking. A lock is identified by one or two keys. The first key generally represent the module name or a specific area within linuxconf. The string used should be unique. The module name is a good choice. Using the module name and a suffix is also a good idea, to differentiate various area within one module. The second key may be anything. It may be a constant, but in general the string will be formatted to identify a record. For example, the locking used for a user account could be something like

        char tmp[100];
        snprintf (tmp,sizeof(tmp),"%s:%s",domain,userid);
        CONTEXT_LOCK l ("useraccount",tmp);
This provides exclusive access to a user account record in a domain. The lock information is stored in the file /var/run/linuxconf.lockdb. This file contains various information about each lock. This includes the process ID, the user id, the terminal and the display of the process owning the lock. This allows the class to generate enough information so you know who is currently owning the lock (probably you on another console :-) ). The class also supports stale lock (the owner process has gone away without removing the lock). The lock is installed by the constructor and removed by the destructor.

#Specbeg: CONTEXT_LOCK / sample ([,89])

            The CONTEXT_LOCK class is used to get exclusive access to some
            area in Linuxconf or a module. This is fully multi-user. This means
            that two Linuxconf instances will cooperate on this.
            The class is very easy to use. It is also interactive. The goal
            is to avoid tedious code repetition here and there.
            The following sample presents two usage. One is locking a whole
            system and the other is locking a specific record. The trick
            is to use the one or two parameters constructor.
        static void sample_context_lock()
            DIALOG_MENU dia;
            static const char *tb[]={
                "test", "global lock",
                "", "lock on record1",
                "", "lock on record2",
            dia.new_menuitems (tb);
            int nof = 0;
            while (1){
                MENU_STATUS code = dia.editmenu ("context-lock"
                    ,"The first option shows the CONTEXT_LOCK with one key\n"
                     "The second and third test the lock with two keys"
                if (code == MENU_QUIT || code == MENU_ESCAPE){
                }else if (nof == 0){
                    CONTEXT_LOCK l("sample");
                    if (l.isok()){
                        xconf_notice ("We have exclusive access");
                }else if (nof == 1 || nof == 2){
                    char tmp[10];
                    sprintf (tmp,"record%d",nof);
                    CONTEXT_LOCK l("sample",tmp);
                    if (l.isok()){
                        xconf_notice ("We have exclusive access on %s",tmp);

18.1 CONTEXT_LOCK Alphabetical function listing

Classe CONTEXT_LOCK, vue publique

Next Previous Contents