+/* codegen_critical_section_new ************************************************
+
+ Allocates a new critical-section reference and adds it to the
+ critical-section list.
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS)
+void codegen_critical_section_new(codegendata *cd)
+{
+ list_t *list;
+ critical_section_ref_t *csr;
+ s4 mpc;
+
+ /* get the critical section list */
+
+ list = cd->listcritical;
+
+ /* calculate the current mpc */
+
+ mpc = cd->mcodeptr - cd->mcodebase;
+
+ csr = DNEW(critical_section_ref_t);
+
+ /* We only can set restart right now, as start and end are set by
+ the following, corresponding functions. */
+
+ csr->start = -1;
+ csr->end = -1;
+ csr->restart = mpc;
+
+ /* add the branch to the list */
+
+ list_add_last_unsynced(list, csr);
+}
+#endif
+
+
+/* codegen_critical_section_start **********************************************
+
+ Set the start-point of the current critical section (which is the
+ last element of the list).
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS)
+void codegen_critical_section_start(codegendata *cd)
+{
+ list_t *list;
+ critical_section_ref_t *csr;
+ s4 mpc;
+
+ /* get the critical section list */
+
+ list = cd->listcritical;
+
+ /* calculate the current mpc */
+
+ mpc = cd->mcodeptr - cd->mcodebase;
+
+ /* get the current critical section */
+
+ csr = list_last_unsynced(list);
+
+ /* set the start point */
+
+ assert(csr->start == -1);
+
+ csr->start = mpc;
+}
+#endif
+
+
+/* codegen_critical_section_end ************************************************
+
+ Set the end-point of the current critical section (which is the
+ last element of the list).
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS)
+void codegen_critical_section_end(codegendata *cd)
+{
+ list_t *list;
+ critical_section_ref_t *csr;
+ s4 mpc;
+
+ /* get the critical section list */
+
+ list = cd->listcritical;
+
+ /* calculate the current mpc */
+
+ mpc = cd->mcodeptr - cd->mcodebase;
+
+ /* get the current critical section */
+
+ csr = list_last_unsynced(list);
+
+ /* set the end point */
+
+ assert(csr->end == -1);
+
+ csr->end = mpc;
+}
+#endif
+
+
+/* codegen_critical_section_finish *********************************************
+
+ Finish the critical sections, create the critical section nodes for
+ the AVL tree and insert them into the tree.
+
+*******************************************************************************/
+
+#if defined(ENABLE_THREADS)
+static void codegen_critical_section_finish(jitdata *jd)
+{
+ codeinfo *code;
+ codegendata *cd;
+ list_t *list;
+ critical_section_ref_t *csr;
+ critical_section_node_t *csn;
+
+ /* get required compiler data */
+
+ code = jd->code;
+ cd = jd->cd;
+
+ /* get the critical section list */
+
+ list = cd->listcritical;
+
+ /* iterate over all critical sections */
+
+ for (csr = list_first_unsynced(list); csr != NULL;
+ csr = list_next_unsynced(list, csr)) {
+ /* check if all points are set */
+
+ assert(csr->start != -1);
+ assert(csr->end != -1);
+ assert(csr->restart != -1);
+
+ /* allocate tree node */
+
+ csn = NEW(critical_section_node_t);
+
+ csn->start = code->entrypoint + csr->start;
+ csn->end = code->entrypoint + csr->end;
+ csn->restart = code->entrypoint + csr->restart;
+
+ /* insert into the tree */
+
+ critical_section_register(csn);
+ }
+}
+#endif
+
+