chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nptl / sysdeps / unix / sysv / linux / x86_64 / cancellation.S
1 /* Copyright (C) 2009 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2009.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <sysdep.h>
21 #include <tcb-offsets.h>
22 #include <kernel-features.h>
23 #include "lowlevellock.h"
24
25 #ifdef IS_IN_libpthread
26 # ifdef SHARED
27 #  define __pthread_unwind __GI___pthread_unwind
28 # endif
29 #else
30 # ifndef SHARED
31         .weak __pthread_unwind
32 # endif
33 #endif
34
35
36 #ifdef __ASSUME_PRIVATE_FUTEX
37 # define LOAD_PRIVATE_FUTEX_WAIT(reg) \
38         movl    $(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), reg
39 #else
40 # if FUTEX_WAIT == 0
41 #  define LOAD_PRIVATE_FUTEX_WAIT(reg) \
42         movl    %fs:PRIVATE_FUTEX, reg
43 # else
44 #  define LOAD_PRIVATE_FUTEX_WAIT(reg) \
45         movl    %fs:PRIVATE_FUTEX, reg ; \
46         orl     $FUTEX_WAIT, reg
47 # endif
48 #endif
49
50 /* It is crucial that the functions in this file don't modify registers
51    other than %rax and %r11.  The syscall wrapper code depends on this
52    because it doesn't explicitly save the other registers which hold
53    relevant values.  */
54         .text
55
56         .hidden __pthread_enable_asynccancel
57 ENTRY(__pthread_enable_asynccancel)
58         movl    %fs:CANCELHANDLING, %eax
59 2:      movl    %eax, %r11d
60         orl     $TCB_CANCELTYPE_BITMASK, %r11d
61         cmpl    %eax, %r11d
62         je      1f
63
64         lock
65         cmpxchgl %r11d, %fs:CANCELHANDLING
66         jnz     2b
67
68         andl    $(TCB_CANCELSTATE_BITMASK|TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK|TCB_EXITING_BITMASK|TCB_CANCEL_RESTMASK|TCB_TERMINATED_BITMASK), %r11d
69         cmpl    $(TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK), %r11d
70         je      3f
71
72 1:      ret
73
74 3:      movq    $TCB_PTHREAD_CANCELED, %fs:RESULT
75         lock
76         orl     $TCB_EXITING_BITMASK, %fs:CANCELHANDLING
77         movq    %fs:CLEANUP_JMP_BUF, %rdi
78 #ifdef SHARED
79         call    __pthread_unwind@PLT
80 #else
81         call    __pthread_unwind
82 #endif
83         hlt
84 END(__pthread_enable_asynccancel)
85
86
87         .hidden __pthread_disable_asynccancel
88 ENTRY(__pthread_disable_asynccancel)
89         testl   $TCB_CANCELTYPE_BITMASK, %edi
90         jnz     1f
91
92         movl    %fs:CANCELHANDLING, %eax
93 2:      movl    %eax, %r11d
94         andl    $~TCB_CANCELTYPE_BITMASK, %r11d
95         lock
96         cmpxchgl %r11d, %fs:CANCELHANDLING
97         jnz     2b
98
99         movl    %r11d, %eax
100 3:      andl    $(TCB_CANCELING_BITMASK|TCB_CANCELED_BITMASK), %eax
101         cmpl    $TCB_CANCELING_BITMASK, %eax
102         je      4f
103 1:      ret
104
105         /* Performance doesn't matter in this loop.  We will
106            delay until the thread is canceled.  And we will unlikely
107            enter the loop twice.  */
108 4:      movq    %fs:0, %rdi
109         movl    $__NR_futex, %eax
110         xorq    %r10, %r10
111         addq    $CANCELHANDLING, %rdi
112         LOAD_PRIVATE_FUTEX_WAIT (%esi)
113         syscall
114         movl    %fs:CANCELHANDLING, %eax
115         jmp     3b
116 END(__pthread_disable_asynccancel)