(timeout_handler): Rewrite ts initialization for C++.
[glibc.git] / nptl_db / td_ta_event_getmsg.c
blob6e68ff4ff29e9613c97dd753514899a6efd53688
1 /* Retrieve event.
2 Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stddef.h>
22 #include <string.h>
24 #include "thread_dbP.h"
27 td_err_e
28 td_ta_event_getmsg (const td_thragent_t *ta_arg, td_event_msg_t *msg)
30 td_thragent_t *const ta = (td_thragent_t *) ta_arg;
31 td_err_e err;
32 psaddr_t eventbuf, eventnum, eventdata;
33 psaddr_t thp, next;
34 void *copy;
36 /* XXX I cannot think of another way but using a static variable. */
37 /* XXX Use at least __thread once it is possible. */
38 static td_thrhandle_t th;
40 LOG ("td_thr_event_getmsg");
42 /* Test whether the TA parameter is ok. */
43 if (! ta_ok (ta))
44 return TD_BADTA;
46 /* Get the pointer to the thread descriptor with the last event. */
47 err = DB_GET_VALUE (thp, ta, __nptl_last_event, 0);
48 if (err != TD_OK)
49 return err;
51 if (thp == 0)
52 /* Nothing waiting. */
53 return TD_NOMSG;
55 /* Copy the event message buffer in from the inferior. */
56 err = DB_GET_FIELD_ADDRESS (eventbuf, ta, thp, pthread, eventbuf, 0);
57 if (err == TD_OK)
58 err = DB_GET_STRUCT (copy, ta, eventbuf, td_eventbuf_t);
59 if (err != TD_OK)
60 return err;
62 /* Read the event details from the target thread. */
63 err = DB_GET_FIELD_LOCAL (eventnum, ta, copy, td_eventbuf_t, eventnum, 0);
64 if (err != TD_OK)
65 return err;
66 /* If the structure is on the list there better be an event recorded. */
67 if ((int) (uintptr_t) eventnum == TD_EVENT_NONE)
68 return TD_DBERR;
70 /* Fill the user's data structure. */
71 err = DB_GET_FIELD_LOCAL (eventdata, ta, copy, td_eventbuf_t, eventdata, 0);
72 if (err != TD_OK)
73 return err;
75 /* Generate the thread descriptor. */
76 th.th_ta_p = (td_thragent_t *) ta;
77 th.th_unique = thp;
79 /* Fill the user's data structure. */
80 msg->msg.data = (uintptr_t) eventdata;
81 msg->event = (uintptr_t) eventnum;
82 msg->th_p = &th;
84 /* And clear the event message in the target. */
85 memset (copy, 0, ta->ta_sizeof_td_eventbuf_t);
86 err = DB_PUT_STRUCT (ta, eventbuf, td_eventbuf_t, copy);
87 if (err != TD_OK)
88 return err;
90 /* Get the pointer to the next descriptor with an event. */
91 err = DB_GET_FIELD (next, ta, thp, pthread, nextevent, 0);
92 if (err != TD_OK)
93 return err;
95 /* Store the pointer in the list head variable. */
96 err = DB_PUT_VALUE (ta, __nptl_last_event, 0, next);
97 if (err != TD_OK)
98 return err;
100 if (next != 0)
101 /* Clear the next pointer in the current descriptor. */
102 err = DB_PUT_FIELD (ta, thp, pthread, nextevent, 0, 0);
104 return err;