{
    "content": [
        {
            "type": "text",
            "text": "# futex (man)\n\n## NAME\n\nfutex - fast user-space locking\n\n## DESCRIPTION\n\nThe  Linux  kernel  provides futexes (\"Fast user-space mutexes\") as a building block for fast\nuser-space locking and semaphores.  Futexes are very  basic  and  lend  themselves  well  for\nbuilding  higher-level  locking abstractions such as mutexes, condition variables, read-write\nlocks, barriers, and semaphores.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS** (1 subsections)\n- **DESCRIPTION** (1 subsections)\n- **VERSIONS**\n- **NOTES**\n- **SEE ALSO**\n- **COLOPHON**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "futex",
        "section": "",
        "mode": "man",
        "summary": "futex - fast user-space locking",
        "synopsis": "",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [
            {
                "name": "clone",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/clone/2/json"
            },
            {
                "name": "getrobustlist",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/getrobustlist/2/json"
            },
            {
                "name": "setrobustlist",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/setrobustlist/2/json"
            },
            {
                "name": "settidaddress",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/settidaddress/2/json"
            },
            {
                "name": "pthreads",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/pthreads/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "#include <linux/futex.h>",
                        "lines": 1
                    }
                ]
            },
            {
                "name": "DESCRIPTION",
                "lines": 22,
                "subsections": [
                    {
                        "name": "Semantics",
                        "lines": 23
                    }
                ]
            },
            {
                "name": "VERSIONS",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "COLOPHON",
                "lines": 7,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "futex - fast user-space locking\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "",
                "subsections": [
                    {
                        "name": "#include <linux/futex.h>",
                        "content": ""
                    }
                ]
            },
            "DESCRIPTION": {
                "content": "The  Linux  kernel  provides futexes (\"Fast user-space mutexes\") as a building block for fast\nuser-space locking and semaphores.  Futexes are very  basic  and  lend  themselves  well  for\nbuilding  higher-level  locking abstractions such as mutexes, condition variables, read-write\nlocks, barriers, and semaphores.\n\nMost programmers will in fact not be using futexes directly but will instead rely  on  system\nlibraries built on them, such as the Native POSIX Thread Library (NPTL) (see pthreads(7)).\n\nA  futex is identified by a piece of memory which can be shared between processes or threads.\nIn these different processes, the futex need not have identical addresses.  In its bare form,\na  futex  has  semaphore  semantics;  it is a counter that can be incremented and decremented\natomically; processes can wait for the value to become positive.\n\nFutex operation occurs entirely in user space for the noncontended case.  The kernel  is  in‐\nvolved  only  to  arbitrate  the  contended case.  As any sane design will strive for noncon‐\ntention, futexes are also optimized for this situation.\n\nIn its bare form, a futex is an aligned integer which is touched only by atomic assembler in‐\nstructions.   This integer is four bytes long on all platforms.  Processes can share this in‐\nteger using mmap(2), via shared memory segments, or because they share memory space, in which\ncase the application is commonly called multithreaded.\n",
                "subsections": [
                    {
                        "name": "Semantics",
                        "content": "Any  futex  operation  starts  in user space, but it may be necessary to communicate with the\nkernel using the futex(2) system call.\n\nTo \"up\" a futex, execute the proper assembler instructions that will cause the  host  CPU  to\natomically increment the integer.  Afterward, check if it has in fact changed from 0 to 1, in\nwhich case there were no waiters and the operation is done.  This is  the  noncontended  case\nwhich is fast and should be common.\n\nIn the contended case, the atomic increment changed the counter from -1  (or some other nega‐\ntive number).  If this is detected, there are waiters.  User space should now set the counter\nto 1 and instruct the kernel to wake up any waiters using the FUTEXWAKE operation.\n\nWaiting on a futex, to \"down\" it, is the reverse operation.  Atomically decrement the counter\nand check if it changed to 0, in which case the operation is done and the  futex  was  uncon‐\ntended.   In  all  other  circumstances, the process should set the counter to -1 and request\nthat the kernel wait for another process to up the futex.  This is done using the  FUTEXWAIT\noperation.\n\nThe  futex(2)  system  call can optionally be passed a timeout specifying how long the kernel\nshould wait for the futex to be upped.  In this case, semantics are more complex and the pro‐\ngrammer  is  referred  to  futex(2)  for more details.  The same holds for asynchronous futex\nwaiting.\n"
                    }
                ]
            },
            "VERSIONS": {
                "content": "Initial futex support was merged in Linux 2.5.7 but with different semantics from  those  de‐\nscribed above.  Current semantics are available from Linux 2.5.40 onward.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "To reiterate, bare futexes are not intended as an easy-to-use abstraction for end users.  Im‐\nplementors are expected to be assembly literate and to have read the  sources  of  the  futex\nuser-space library referenced below.\n\nThis  man  page illustrates the most common use of the futex(2) primitives; it is by no means\nthe only one.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "clone(2), futex(2), getrobustlist(2), setrobustlist(2), settidaddress(2), pthreads(7)\n\nFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux (proceedings of the Ottawa  Linux\nSymposium  2002),  futex  example  library,  futex-*.tar.bz2  ⟨ftp://ftp.kernel.org/pub/linux\n/kernel/people/rusty/⟩.\n",
                "subsections": []
            },
            "COLOPHON": {
                "content": "This page is part of release 5.10 of the Linux  man-pages  project.   A  description  of  the\nproject,  information about reporting bugs, and the latest version of this page, can be found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\nLinux                                        2017-09-15                                     FUTEX(7)",
                "subsections": []
            }
        }
    }
}