1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php

/**
*   Quick and Dirty File Mutex
*
*   Copyright(c)2013 SwingNote LLC. All Rights Reserved.
*
*   Terms: You may freely use this source code in both
*   commercial and non-commercial applications, provided
*   all copyright notices remain intact.
*
*   Author:     Markus Diersbock <markus@swingnote.com>
*/
function mutex($action) {
    $mutex = "lock.mutex";

    try {
        switch ($action) {
            case "check":
                return file_exists($mutex);

            case "create":
                return mutex("check") ? false : touch($mutex);

            case "delete":
                return mutex("check") ? unlink($mutex) : false;

            default:
                return false;
        }
    } catch (Exception $e) {
        return false;
    }
}